How can I make a partial view reusable across viewmodels (asp.net mvc 2)?

I created a partial view that I would like to use for reuse on different pages of my site.

Currently, for each web page, I am creating a ViewModel that matches all the information I would like to show, and then sending it to a view that is compromised from 3 separate partial views. Here is a simplified example:

public ActionResult Index()
    {
        MyViewModel m = new MyViewModel();
        MyViewModel modelData = m.GetModelData();
        return View(modelData);
    }

public MyViewModel GetModelData()
    {
        ModelData modelData = new ModelData();
        modelData.employees = GetEmployees();
        modelData.products = GetProducts();
        modelData.prices = GetPrices();
        return modelData
    }

On my web page, every partial view inherits from the same presentation model, which allows me to strongly print each partial view:

Inherits="System.Web.Mvc.ViewUserControl<DomainModel.Entities.MyViewModel>

, ( " .ascx" ) -, . , DomainModel.Entities.MyViewModel, , , . CheckOutViewModel.

, ? asp.net mvc 2, - ?

- , - ...

+3
1

, . , . , RenderPartial, .

public MyViewModel GetModelData()
    {
        ModelData modelData = new ModelData();
        modelData.employees = GetEmployees();
        modelData.products = GetProducts();
        modelData.MyPricesViewModel = GetPrices();   <-- this should be a view model
        return modelData
    }

, , .

Inherits="System.Web.Mvc.ViewUserControl<DomainModel.Entities.MyPricesViewModel>

:

<% Html.RenderPartial("Prices", Model.MyPricesViewModel); %>
+2

Source: https://habr.com/ru/post/1768197/


All Articles