I am developing a website using the Asp.Net MVC framework. We need to add general user information across several pages, similar to the reputation bar at the top.
I find it important that there is no overhead associated with creating a controller method to add this additional information to the view. This eliminates the possibility of transmitting this information in a ViewData object or changing ViewModels to accept a reputation field, as this will cause each controller to look like this:
public ActionResult Index()
{
ViewData["reputationScore"] = GetUsersReputation(userId);
}
If it is used on 90% of the pages on the site, it may take a long time to change if we also wanted to display the number of user icons.
I can think of 4 solutions to this
Use the master page
Get the user's reputation in the code of the main page and put a mark for reputation on the main page.
Disadvantages:
- It seems to move away from everything that MVC represents.
- I was looking for a switch to using alternative viewing mechanisms (e.g. razors). I'm not sure how well they will mix.
- It limits the layout of your reputation - it's hard to put it in the middle of the page.
Extend HtmlHelper to add the GetUsersReputation method . This, apparently, is a small violation of what the Html object should be used for - this is not just output output, but getting into the database. I canโt think of any other significant issues besides breaking the metaphor.
System.Web.Mvc.ViewPage
Viewpage Html, , . , , HtmlHelper, , , . - :
<% DataAccess.GetUsersReputation() %>
, , , :
public ActionResult Index()
{
MyViewModel viewCoreInfo = model.GetData();
return View(new BaseViewModel<MyViewModel>(viewCoreInfo));
}
BaseViewModel , -. UsersReputation ( , ). , MVC , .