asp.net-mvc 如何将模型传递给局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21172595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass model to partial view
提问by DarkNik
I have two view models:
我有两个视图模型:
public class ParentViewModel
{
public Id { get; set; }
.....
public ChildViewModel Child{ get; set; }
}
public class ChildViewModel
{
public ChildId { get; set; }
.....
}
Controllers:
控制器:
public ActionResult Index()
{
.... <some code>
return View("NewIndex", ParentViewModel);
}
[HttpPost]
public ActionResult PartialAction(ChildViewModel childView)
{
return RedirectToAction("Index");
}
And views: Index
和意见:索引
@model ParentViewModel
....
@Html.Partial("_Partial", Model.Child)
and _Partial
和_部分
@model ChildViewModel
... do some stuff with child model
When I'm trying to open Index page I've got an error:
当我尝试打开索引页面时出现错误:
The model item passed into the dictionary is of type 'ParentViewModel', but this dictionary requires a model item of type 'ChildViewModel'.
传递到字典中的模型项的类型为“ParentViewModel”,但该字典需要一个“ChildViewModel”类型的模型项。
Why it tries to pass ParentViewModel instead of ChildViewModel. What I'm doing wrong?
为什么它试图传递 ParentViewModel 而不是 ChildViewModel。我做错了什么?
回答by Dev
I had the same issue as the OP. From one of the comments, I realized that the second parameter shouldn't be null, i.e from
我和 OP 有同样的问题。从评论之一,我意识到第二个参数不应该为空,即来自
@model ParentViewModel
@Html.Partial("_Partial", Model.Child)
If Model.Child is null, then Modelis passed instead of Model.Child. If there will be cases when the second parameter is null, then you will have to check first in your code and maybe pass an initialized Child as the second parameter. Something like this
如果 Model.Child 为 null,则传递Model而不是Model.Child。如果出现第二个参数为空的情况,那么您必须首先检查代码,并可能将初始化的 Child 作为第二个参数传递。像这样的东西
@Html.Partial("_Partial", new Child())
回答by DarkNik
The answer is that needs to pass an empty object to Partial, like
答案是需要传递一个空对象给 Partial,比如
@Html.Partial("_Partial", new ChildViewModel ())
回答by dey.shin
You could return PartialView("...")from a Controllerinstead, and call the action from the Index view.
您可以PartialView("...")从 a返回Controller,然后从 Index 视图调用操作。
Controllers:
控制器:
public ActionResult Index()
{
.... <some code>
return View("NewIndex", ParentViewModel);
}
public ActionResult Partial(ChildViewModel cvm)
{
var vm = cvm ?? new ChildViewModel(); //if cvm from the parent view is null, return new cvm
return PartialView("_Partial", vm) //return partial view
}
[HttpPost]
public ActionResult PartialAction(ChildViewModel childView)
{
return RedirectToAction("Index");
}
And Index
和索引
@model ParentViewModel
....
@Html.Action("Partial", Model.Child)
Alternatively, you could initialize ParentViewModel in the Index()public ActionResult Index()
或者,您可以在Index()公共 ActionResult Index() 中初始化 ParentViewModel
{
.... <some code>
return View("NewIndex", new ParentViewModel{Child = new ChildViewModel});
}

