MVC - passing additional information to a view

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

    // main controller logic here
}

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 , .

  • -
  • - , /?
+3
5

, , ScottGu.

"" , ( "Masterpage" ), . SO .

EDIT:

... mvc music store .

Html.RenderAction. Html.RenderPartial - . question, , .

:

 return PartialView();
+3

MyViewModel BaseViewModel?

BaseController.OnResultExecuting :

protected override void OnResultExecuting(ResultExecutingContext ctx) {
    base.OnResultExecuting(ctx);
    var baseView = ctx.Result as BaseViewModel;
    if (baseView != null)
    {
        //assign values here
    }
}
+3

You can create an action filter, override OnResultExecuting, fill in the general values โ€‹โ€‹and add it to your action / controller:

public class AddCommonData : ActionFilterAttribute 
{
  public override void OnResultExecuting(ResultExecutingContext filterContext)
  {
    ViewResult viewResult = filterContext.Result as ViewResult;
    if (viewResult != null)
    {
      //...
    } 
  }
}
+1
source

I did this, and I created a base model class with all the information. All my other class viewmodel inherits from this. But you must fill in this information in each controller. This is the solution I have chosen.

0
source

You can create a partial view with a codebehind file in which you will populate the partial view with data. This is a little non-mvc way, but it will work.

0
source

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


All Articles