How to pass model to partial view
Asked Answered
B

3

43

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?

Blush answered 16/1, 2014 at 20:51 Comment(3)
When calling is Model.Child null? Because the second parater of the Html.Partial should not be null... So make you that your Child property has some value!Giliane
actually my Partial view is only for AddObject functionality, so ChildViewModel needs only for sending data to controller from PartialView. So do noot need to pass data in Child from Index to Partial viewBlush
Still you need to pass in a ChildViewModel to the @Html.Partial call. However it can be an empty object : @Html.Partial("_Partial", new ChildViewModel ())...Giliane
C
55

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

@model ParentViewModel
@Html.Partial("_Partial", Model.Child)

If Model.Child is null, then Model is 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

@Html.Partial("_Partial", new Child())
Cryostat answered 31/7, 2015 at 19:57 Comment(2)
Struggled with this forever not realizing a null child would result in the same error. Thanks!Eldaelden
How about @Html.Partial("_Partial", Model.Child ?? new Child()) ?Sinner
B
14

The answer is that needs to pass an empty object to Partial, like

@Html.Partial("_Partial", new ChildViewModel ())
Blush answered 17/1, 2014 at 8:10 Comment(0)
S
2

You could return PartialView("...") from a Controller instead, and call the action from the Index view.

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()

{
    .... <some code>
    return View("NewIndex", new ParentViewModel{Child = new ChildViewModel});
}
Schroth answered 9/1, 2018 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.