PartialView中的页面重定向
在MVC的每个action中,都可以指定一种返回页面的类型,可以是ActionResult,这表示返回的页面为view或者是一个PartialView,前台是一个全整页面,后台是页面的一部分。
在以ASPX为页面引擎时,PartialView被称为分部视图,扩展名为ASCX,与webform中的用户控件是一样的,即页面中的一个部分;而使用razor为页面引擎时,PartialView扩展名还是cshtml,这一点感觉与普通页面有些混乱。不过,这不是今天我要讲的重点,今天的重点间在partialview中进行页面重定向的方式。
第一种情况:在PartialView中进行表单提示操作后,需要返回别一个PartialView来填充原来的这个PartialView的内容
这种情况需要我们的action返回值类型必须是PartialViewResult,返回代码必须是PartialView
代码如下:
1 public PartialViewResult ApplyRegister(User_Register_ApplyModel entity) 2 { 3 User_Register_Apply user_Register_Apply = new User_Register_Apply(); 4 TryUpdateModel(user_Register_Apply); 5 if (ModelState.IsValid) 6 { 7 user_Register_Apply.UserID = VCommons.Utils.GetNewGuid(); 8 VM = user_InfoManager.ApplyRegister(user_Register_Apply); 9 if (! VM.IsComplete) 10 { 11 VM.ToList().ForEach(i => ModelState.AddModelError( "" , i)); 12 } 13 else 14 return PartialView( " ApplySuccess " , entity.Email); // 返回到指定的PartialView,它将替换 ApplyRegister 这个视图内容 15 } 16 return PartialView(); 17 }
第二种情况,在PartialView视图中提交表单,然后使整个页面进行一个跳转,需要注意的是不能用response.redirect,而必须用JS的location.href,前者会在本partial位置进行跳换。
代码如下:
1 public PartialViewResult UserLogOn(UserLogOnModel entity) 2 { 3 if (ModelState.IsValid) 4 { 5 if (LogOn( new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete) 6 { 7 Response.Write( " <script>location.href='home/index';</script> " ); // 在ascx中跳到指定页,需要用JS方法 8 } 9 } 10 return PartialView(); 11 }
第三种情况,也是最简单的一种情况,在partialview中只是一个链接,没有提交动作,只是将partialview的部分进行重定向,这里代码使用response.redirect()即可
代码如下:
1 public PartialViewResult UserLogOn(UserLogOnModel entity) 2 { 3 if (ModelState.IsValid) 4 { 5 if (LogOn( new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete) 6 { 7 Response.Redirect( " /home/index " ); 8 } 9 } 10 return PartialView(); 11 }
个人建议,对于partialview的action,如果只是返回视图,而不是返回json和其它格式的对象,最好使用 PartialViewResult 进行返回,而不要使用ActionResult,这样可以避免一些不
必要的麻烦。
爱上MVC3目录
爱上MVC3系列~Razor模板页
爱上MVC3系列~全局异常处理与异常日志
爱上MVC3系列~使用视图模型的好处及与数据模型之间的赋值问题
爱上MVC3系列~RenderAction与RenderPartial及一个页面多个表单提交
爱上MVC3系列~Html.BeginForm与Ajax.BeginForm
爱上MVC3系列~PartialView中的页面重定向
爱上MVC3系列~PartialView中的页面重定向
分类: .NET MVC
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于PartialView中的页面重定向的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did48670