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'.
Why it tries to pass ParentViewModel instead of ChildViewModel. What I'm doing wrong?
Model.Child
null? Because the second parater of theHtml.Partial
should not benull
... So make you that yourChild
property has some value! – Giliane@Html.Partial
call. However it can be an empty object :@Html.Partial("_Partial", new ChildViewModel ())
... – Giliane