MVC3 pass @model to partial view

回眸只為那壹抹淺笑 提交于 2019-12-03 13:05:21

You can create a ViewModel Z, which passed to View. If you want to pass model X or Y, just passed that to Z then pass model Z to View.

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);
 }

Make both classes implement the same interface, and use the interface as your model.

As suggested by Tim: If possible you could also inherit from the same base class. Although this is not always possible, using the interface approach is mostly possible.

Mostly like gandil: Create a ViewModel Z but use Automapper to map from Y and X. That way you can keep your UI models clean and DRY.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!