Retrieving data in partial view or layout using MVC

I want to display information about a registered user (username, company name, number of notifications, etc.) in the layout and / or partial view. They will be distributed on every page. Is there a trick to getting this data for them, or is it a case of expanding each model to have this information in them?

+5
source share
2 answers

I would suggest that you can perform a child action and call it from the layout. Thus, you can avoid the execution of information in all viewing models.

Ref.

Action for children

public class UserController
{
  [ChildActionOnly]
  public PartialViewResult UserInfo()
  {
     var userInfo = .. get the user information from session or db

     return PartialView(userInfo);
  }
}

Partial view

@model UserInfoModel

@Html.DisplayFor(m => m.UserName)
@Html.DisplayFor(m => m.CompanyName)
...

Layout view

<header>
  @Html.Action("UserInfo", "User")
</header>
+22
source

MVC, . , , MVC

0

All Articles