MVC3 pass @model to partial view

TheGwa picture TheGwa · Jun 21, 2011 · Viewed 8.4k times · Source

I have two partial views which are exactly the same, but for the @model.

@model Project.Models.X

@model Project.Models.Y

How could I pass this model type to the view so that I can use the same view for both?

Answer

mymex1 picture mymex1 · Jun 22, 2011

Not sure if this is best practice, but you could also use HTML.RenderAction to call your controller and have it return a PartialViewResult with whatever model you want, like so:

  @{Html.RenderAction("MyPartialAction", "MyController", new { someID = 1 });}

and

 public PartialViewResult MyPartialAction(int? someID)
 {
        return PartialView("MyPartial",SomeModel);
 }