ASP.NET MVC 之PartialView用法
2013-06-09 17:25
387 查看
ASP.NET MVC 之PartialView用法
第一种情况:
PartialView中进行表单提示操作后,需要返回别一个PartialView来填充原来的这个PartialView的内容。这种情况需要我们的action返回值类型必须是PartialViewResult,返回代码必须是PartialView
代码如下:
view plaincopy to clipboardprint?
public PartialViewResult ApplyRegister(User_Register_ApplyModel entity)
{
User_Register_Apply user_Register_Apply = new User_Register_Apply();
TryUpdateModel(user_Register_Apply);
if (ModelState.IsValid)
{
user_Register_Apply.UserID = VCommons.Utils.GetNewGuid();
VM = user_InfoManager.ApplyRegister(user_Register_Apply);
if (!VM.IsComplete)
{
VM.ToList().ForEach(i => ModelState.AddModelError("", i));
}
else
return PartialView("ApplySuccess", entity.Email);//返回到指定的PartialView,它将替换ApplyRegister这个视图内容
}
return PartialView();
}
public PartialViewResult ApplyRegister(User_Register_ApplyModel entity)
{
User_Register_Apply user_Register_Apply = new User_Register_Apply();
TryUpdateModel(user_Register_Apply);
if (ModelState.IsValid)
{
user_Register_Apply.UserID = VCommons.Utils.GetNewGuid();
VM = user_InfoManager.ApplyRegister(user_Register_Apply);
if (!VM.IsComplete)
{
VM.ToList().ForEach(i => ModelState.AddModelError("", i));
}
else
return PartialView("ApplySuccess", entity.Email);//返回到指定的PartialView,它将替换ApplyRegister这个视图内容
}
return PartialView();
}
第二种情况:
在PartialView视图中提交表单,然后使整个页面进行一个跳转,需要注意的是不能用response.redirect,而必须用Jlocation.href,前者会在本partial位置进行跳换。
代码如下:
view plaincopy to clipboardprint?
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Write("<script>location.href='home/index';</script>");//在ascx中跳到指定页,需要用JS方法
}
}
return PartialView();
}
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Write("<script>location.href='home/index';</script>");//在ascx中跳到指定页,需要用JS方法
}
}
return PartialView();
}
第三种情况:
也是最简单的一种情况,在partialview中只是一个链接,没有提交动作,只是将partialview的部分进行重定向,这里代码使response.redirect()即可
代码如下:
view plaincopy to clipboardprint?
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Redirect("/home/index");
}
}
return PartialView();
}
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Redirect("/home/index");
}
}
return PartialView();
}
个人建议,对于partialview的action,如果只是返回视图,而不是返回json和其它格式的对象,最好使用PartialViewResult 进行返回,而不要使用ActionResult,这样可以避免一些不必要的麻烦。
第一种情况:
PartialView中进行表单提示操作后,需要返回别一个PartialView来填充原来的这个PartialView的内容。这种情况需要我们的action返回值类型必须是PartialViewResult,返回代码必须是PartialView
代码如下:
view plaincopy to clipboardprint?
public PartialViewResult ApplyRegister(User_Register_ApplyModel entity)
{
User_Register_Apply user_Register_Apply = new User_Register_Apply();
TryUpdateModel(user_Register_Apply);
if (ModelState.IsValid)
{
user_Register_Apply.UserID = VCommons.Utils.GetNewGuid();
VM = user_InfoManager.ApplyRegister(user_Register_Apply);
if (!VM.IsComplete)
{
VM.ToList().ForEach(i => ModelState.AddModelError("", i));
}
else
return PartialView("ApplySuccess", entity.Email);//返回到指定的PartialView,它将替换ApplyRegister这个视图内容
}
return PartialView();
}
public PartialViewResult ApplyRegister(User_Register_ApplyModel entity)
{
User_Register_Apply user_Register_Apply = new User_Register_Apply();
TryUpdateModel(user_Register_Apply);
if (ModelState.IsValid)
{
user_Register_Apply.UserID = VCommons.Utils.GetNewGuid();
VM = user_InfoManager.ApplyRegister(user_Register_Apply);
if (!VM.IsComplete)
{
VM.ToList().ForEach(i => ModelState.AddModelError("", i));
}
else
return PartialView("ApplySuccess", entity.Email);//返回到指定的PartialView,它将替换ApplyRegister这个视图内容
}
return PartialView();
}
第二种情况:
在PartialView视图中提交表单,然后使整个页面进行一个跳转,需要注意的是不能用response.redirect,而必须用Jlocation.href,前者会在本partial位置进行跳换。
代码如下:
view plaincopy to clipboardprint?
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Write("<script>location.href='home/index';</script>");//在ascx中跳到指定页,需要用JS方法
}
}
return PartialView();
}
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Write("<script>location.href='home/index';</script>");//在ascx中跳到指定页,需要用JS方法
}
}
return PartialView();
}
第三种情况:
也是最简单的一种情况,在partialview中只是一个链接,没有提交动作,只是将partialview的部分进行重定向,这里代码使response.redirect()即可
代码如下:
view plaincopy to clipboardprint?
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Redirect("/home/index");
}
}
return PartialView();
}
public PartialViewResult UserLogOn(UserLogOnModel entity)
{
if (ModelState.IsValid)
{
if (LogOn(new User_Info { Email = entity.Email, Password = entity.Password }).IsComplete)
{
Response.Redirect("/home/index");
}
}
return PartialView();
}
个人建议,对于partialview的action,如果只是返回视图,而不是返回json和其它格式的对象,最好使用PartialViewResult 进行返回,而不要使用ActionResult,这样可以避免一些不必要的麻烦。
相关文章推荐
- ASP.NET MVC PartialView用法
- Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
- Asp.net mvc partialView
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法(mvc部分视图的添加)
- 在asp.net mvc中使用PartialView返回部分HTML段
- ASP.Net MVC开发基础学习笔记(10):分部视图PartialView
- 在asp.net mvc中使用PartialView返回部分HTML段
- 在asp.net mvc中使用PartialView返回部分HTML段
- [转]Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
- Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
- 在asp.net mvc中使用PartialView返回部分HTML段
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
- 在Asp.Net MVC中PartialView与EditorFor和DisplayFor的区别
- Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
- asp.net MVC ViewData用法
- 在asp.net mvc中使用PartialView返回部分HTML段
- Asp.Net MVC 把PartialView、View转换成字符串
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法