我正在研究使用Razor在MVC3中使用部分视图,并且我获得了部分视图来呈现,并且工作正常。 但是,我想要做的是在部分视图提交时刷新父视图。当提交部分视图的表单时刷新父视图
代码在我父视图呈现局部视图
<div id="mydiv">
@{ Html.RenderAction("Add", "Request"); }
</div>
父视图操作简单,
public ActionResult Index()
{
List<obj> reqs = //some query
return View(reqs);
}
在我的部分观点的取得动作,我有:
public ActionResult Add()
{
AddRequestViewModel vm = new AddRequestViewModel();
//set some stuff on the VM here
return PartialView(vm);
}
在部分视图调用的后处理操作中,如果modelstate无效,则return PartialView(vm)
如果它是有效的,我想要刷新父视图和部分视图。 我尝试RedirectToAction,但这不能由部分,显然,我试图return Index();
称为一个动作被调用,而这会导致用于呈现局部视图代码中的问题,
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'.
如何做到这一点的任何建议,将不胜感激。页面的目的是显示一个元素列表,并且partial包含一个向列表中添加一个新元素的表单。
Edit:
该部分的模型是不同的,因为它包含选择数据,这是从数据库,这就是为什么我尝试了RenderAction,但我不知道是否有其他方式做到这一点。
我没有看到你在这种情况下需要Ajax请求。 –