Passing model to Html.Action

2019-04-30 16:35发布

问题:

Using asp.net MVC3, I have a partial view which requires remote validation. As I understand it this means it needs it's own controller, so I'm using Html.Action to call this view and it's controller.

I do however wish to still pass a model to Html.Action, just like I would with Html.Partial. How can I do this?

回答1:

Html.Action has an overload that expects route values as an object. you could trying passing the model there and model binding will kick in. Your action has to be expecting a parameter of type Model though.

Html.Action("ActionName","ControllerName", Model)


回答2:

You must pass the model with anonymous classes

    @Html.Action("Menu", "MyController", new { data = Model.Foo.Bar})

    [ChildActionOnly]
    public ActionResult Menu(Bar data )
    {
        return PartialView("Menu", data );
    }