提交部分视图表单时刷新父视图
我正在研究使用 Razor 在 MVC3 中使用部分视图,并且我得到了我的部分视图来渲染并且它工作得很好。 不过,我想做的是在提交部分视图时刷新父视图。
我的父视图中用于渲染部分视图的代码
<div id="mydiv">
@{ Html.RenderAction("Add", "Request"); }
</div>
父视图的操作很简单,
public ActionResult Index()
{
List<obj> reqs = //some query
return View(reqs);
}
在我的部分视图的 get 操作中,我有:
public ActionResult Add()
{
AddRequestViewModel vm = new AddRequestViewModel();
//set some stuff on the VM here
return PartialView(vm);
}
在部分视图调用的后操作中,如果 modelstate 无效,则返回 PartialView(vm)
如果它有效,我希望刷新父视图和部分视图。 我尝试了 RedirectToAction,但显然不能在由部分调用的操作中调用它,并且我尝试了 return Index();,但这会导致用于渲染部分的代码出现问题view,
异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“System.Collections.Generic.List”1[DatRequests.Models.ReqRequest]”,但此字典需要一个模型类型为“DatRequests.ViewModels.AddRequestViewModel”的项目。
对于如何执行此操作的任何建议,我们将不胜感激。页面的目的是显示元素列表,而部分包含一个用于向列表添加新元素的表单。
编辑:
部分的模型是不同的,因为它包含来自数据库的选择数据,这就是我尝试 RenderAction 的原因,但我不确定是否还有其他方法可以做到这一点。
I'm looking into using partial views in MVC3 using Razor, and I get my partial view to render and it works fine.
What I'd like to do, though, is refresh the parent view when the partial view is submitted.
Code in my parent view to render partial view
<div id="mydiv">
@{ Html.RenderAction("Add", "Request"); }
</div>
Action for parent view is simple,
public ActionResult Index()
{
List<obj> reqs = //some query
return View(reqs);
}
In my partial view's get action I have:
public ActionResult Add()
{
AddRequestViewModel vm = new AddRequestViewModel();
//set some stuff on the VM here
return PartialView(vm);
}
In the post action called by the partial view, if modelstate isn't valid, return PartialView(vm)
If it is valid, I'd like the parent and partial views to refresh.
I tried RedirectToAction, but this can't be called in an action called by a partial, apparently, and I tried return Index();
, but this causes an issue with the code used to render the partial view,
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[DatRequests.Models.ReqRequest]', but this dictionary requires a model item of type 'DatRequests.ViewModels.AddRequestViewModel'.
Any suggestions on how to do this would be appreciated. The purpose of the page is to show a list of elements, and the partial contains a form to add a new element to the list.
Edit:
The partial's model is different, as it contains data for selection, which is from a db, which is why I tried RenderAction, but I'm not sure if there are other ways of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当部分视图正常提交时,您将其提交给某些控制器操作。您可以使用普通请求或 AJAX 请求来提交它。如果您使用普通请求,则可以执行标准重定向到 POST 控制器操作内的
Index
,该操作将处理表单提交。如果您使用 AJAX,您可以返回一个指向您要重定向的 url 的 JSON 结果:并在 AJAX 成功回调中:
When the partial view is submitted normally you submit it to some controller action. You could either submit it using a normal request or an AJAX request. If you use a normal request you could perform a standard redirect to the
Index
inside the POST controller action that will handle the form submission. If you use AJAX, you could return a JSON result pointing to the url that you want to redirect:and inside your AJAX success callback:
也许 RenderAction 不应该以这种方式使用。
当使用 Html.RenderAction 时,一个新的/单独的请求将被发送到服务器。您还有另一个机会从数据库或其他地方加载一些数据以显示给客户端。另外,您可以将 OutputCache 应用于此操作。这通常是进行全局缓存的方式。
在这里,您正在向服务器执行 POST。要么直接在此处放置一个元素,要么使用部分视图来进行 Post。并在对应的action中,做一个RedirectToAction。
是否用ajax 来做不是重点。我的意见更多的是关于使用 RenderAction 的正确方法
Probably RenderAction should not be used this way.
When using Html.RenderAction, a new/seperate request would be sent to the server. And you got another chance to load some data from db or somewhere else to display to the client. Also, you could apply OutputCache to this action. this is usually the way doing global cache.
Here you are doing a POST to the server. Either directly put a element here or using a partial view to do the Post. And in the corresponding action, do a RedirectToAction.
Do it with ajax or not isn't the point. my opinion is more about the right way using RenderAction