MVC小系列(十)【PartialView中的页面重定向】


在mvc的每个Action中,都可以指定一种返回页面的类型,可以是ActionResult,这表示返回的页面为View或者是一个PartialView,

在以Aspx为页面引擎时,PartialView被称为分部视图,扩展名为ASCX,与webform的用户控件一样,是页面的一部分,而使用Razor为页面引擎时,partialView扩展名还是cshtml

第一种情况,在Partialview中进行表单提示操作后,需要返回一个PartialView来填充原来的partialView的内容,这种情况需要action返回值类型必须是 PartialviewResult,返回代码必须是Partialview

1  public PartialViewResult Other()
2         {
3             return PartialView("Some",entity);//返回指定的分部视图
4         }

第二种情况:
在Partialview视图中提交表单,然后使整个页面进行一个跳转,这里不能使用response.redirect,而必须用js的location.href,前者会在本Partial位置进行跳转

 1  public PartialViewResult UserLogOn(UserLogOnModel entity)
 2          {
 3              if (ModelState.IsValid)
 4              {
 5         
 6                  Response.Write("<script>location.href='home/index';</script>");//在ascx中跳到指定页,需要用JS方法
 7                  
 8              }
 9              return PartialView();
10          }

第三种情况:在Partialview中只是一个链接,没有提交动作,只是将Partialview的部分进行重定向,这里用response.redirect

1  public PartialViewResult UserLogOn(UserLogOnModel entity)
2         {
3             if (ModelState.IsValid)
4             {
5                 Response.Redirect("/home/index");
6             }
7             return PartialView();
8         }


注意:对于Partialview的action,如果只是返回视图,而不是返回json或者其他格式的对象,最好使用PartialviewResult进行返回,而不要使用ActionResult

原文地址:https://www.cnblogs.com/niuzaihenmang/p/5624149.html