ASP.NET MVC Home Page Data

The more I use ASP.NET MVC, the more I like it. However, when displaying model data on the main pages, there are several ways to do this. I am not sure about the best solution.

My example would be a commerce site on which I want to list the categories of products on each page, as well as show the status of the visitors basket.

In asp.net web forms, I usually do this with custom controls, each of which performs its own data binding to obtain the required data.

In MVC, all data must be transmitted by the controller.

Thus, with regard to categories, the simplest solution would seem to be to convey this in the data representation in the controller action:

ViewData["Categories"] = _service.GetCategories(); 

However, doing this for each action is not very DRY, so follow this article. I created a basic controller that adds the required data to my ViewData:

  public class AppController : Controller { IAppService _service; public AppController() { } public AppController(IAppService appService) { _service = appService; SetSiteData(); } private void SetSiteData() { ViewData["Categories"] = _service.GetCategories(); } } 

Then I created an extension for ViewMasterPage:

  public static void RenderCategoryList(this ViewMasterPage pg) { pg.Html.RenderPartial("CategoryList", pg.ViewData["Categories"]); } 

And in my MasterPage:

  <div> <%this.RenderCategoryList(); %> </div> 

This seems like a pretty clean approach. However, this is the best way, as I also saw suggestions for creating a ViewModel for your MasterPage. I could see that perhaps as your ViewModel data grows this might be the best solution.

Regarding the status of the cart, I suppose I would do something similar, but I'm not sure if RenderAction would be more appropriate ( When to use RenderAction vs RenderPartial with ASP.NET MVC ). Thanks Ben

+4
source share
2 answers

This works, although it is not the way I would do it for two reasons:

  • I don’t like pasting data into the ViewState, since you essentially make it as an object
  • By requiring a base controller, you limit the functionality of the controllers that inherit this base controller (which may not be a problem)

I think this would be the ideal use of RenderAction (part of the MvcFutures project). This helper can be used to visualize actions on another controller. So you can have a ProductController with the ListCategories action, you can just do something like:

 <% Html.RenderAction<ProductController>(x => x.ListCategories()); %> 

ListCategories will call

 _service.GetCategories(); 

and can embed information in its own model. Then he will pass this model into the view, it will be a partial page.

+7
source

Thanks - RenderAction was a great job.

I got more information from here .

So, in the interests of others, here is an example of how I deduced the status of the basket:

Act:

  [ChildActionOnly] public ActionResult CartStatus() { return PartialView(_service.GetCartSummary()); } 

Partial view (related to Models.Cart)

 <div class="cartSummary"> <%if (Model.HasItems) { %> Cart Items: <%=Model.Items.Count() %> | Total: <%=Model.TotalItems %> <%} else {%> Your cart is empty. Please buy stuff! <%} %> 

Helper method for the main page:

  public static void RenderCartStatus(this ViewMasterPage pg) { pg.Html.RenderAction("CartStatus", "Catalog", null); } 

And finally, the main page:

 <%this.RenderCartStatus(); %> 

Thanks for the advice.

+2
source

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


All Articles