23

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 view:

Html.RenderAction("Index", "ViewName");

Trying this doesn't sent the model object, i'm getting nulls instead (the object has data, and the view expects it):'

Html.RenderAction("Index", "ViewName", objectModel);

Is this even possible using RenderAction?

Thanks!

Edit: I found the error, there was an error with the controller's action that didn't pick up the sent object. Thanks for all your help!

tereško
  • 56,151
  • 24
  • 92
  • 147
Michael
  • 287
  • 1
  • 2
  • 10

3 Answers3

35

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.

Rajmond Burgaj
  • 2,984
  • 2
  • 20
  • 44
theJerm
  • 4,192
  • 2
  • 24
  • 23
27

say you want to pass foo as model, make it first

public class Foo {
    public string Name { get; set; }
    public int Age { get; set; }
}

now make an ActionResult

public ActionResult FooBar(Foo _foo){
    return PartialView(_foo);
}

call it

@Html.RenderAction("FooBar", "Controller", new { Name = "John", Age=20 });
Community
  • 1
  • 1
John x
  • 3,911
  • 8
  • 39
  • 66
  • Thanks for your answer. I had an error that I overlooked in the Controller's Action signature. Fixed it and now it's getting the object. – Michael Jan 22 '12 at 06:54
  • Does this pass ModelState? – aelstonjones Dec 20 '12 at 22:34
  • Thanks! But I had to surround Html.RenderAction call with curly braces to make it work. Otherwise, Razor says "Cannot implicitly convert type 'void' to 'object'" – burkay Dec 04 '19 at 16:26
6

Usually if I have a model already available it makes more sense to use Html.Partial than trying to render an action.

@Html.Partial("Foo", Model.FooModel)

Where Foo.cshtml is a view file (perhaps in your Shared folder) strongly typed with with @model FooProject.Models.FooModel or whatever your model is called. This can be as complex a model as you need it to be. Model is your page's main model into which you must set FooModel - or just omit this parameter if the Foo view uses the same model as the parent page.

RenderAction is generally better when you have just simple parameters, because you're just simulating a request to a regular action which has routing/query string parameters - and then dumping that response into your page. It works well if you need to put something in a Layout that isn't available in your page's model such as an element in a side bar.

Simon_Weaver
  • 120,240
  • 73
  • 577
  • 618