How to send model object in Html.RenderAction (MVC3)

后端 未结 3 1814
我寻月下人不归
我寻月下人不归 2020-12-15 04:59

I\'m using MVC3 razor, and I\'m trying to pass an object to a partial view, and it\'s not working.

This works fine without sending the object model to the partial vi

3条回答
  •  无人及你
    2020-12-15 05:16

    You can actually pass an object to a controller method using Action. This can be done on any avaialble view, for instance I have one in a shared library that gets built to project bin folders that reference my shared project (properties - Copy if newer on the view file, in Visual Studio). It is done like so:

    Controller:

    public class GroovyController : Controller
    {
        public ActionResult MyTestView(MyModel m)
        {
            var viewPath = @"~\bin\CommonViews\MyTestView";
            return View(viewPath, m);
        }
    }
    

    MVC page (using Razor syntax):

    @Html.Action("MyTestView", "Groovy", new { m = Model })
    

    or using RenderAction method:

    @{ Html.RenderAction("MyTestAction", "MyTestController", new { area = "area", m = Model }); }
    

    Note: in the @Html.Action(), the Model object must be of type MyModel and that 3rd parameter must be set to the controller variable name, of which mine is MyModel m. The m is what you must assign to, so I do m = Model.

提交回复
热议问题