This is probably a dumb question but I am trying to figure out how to populate a ViewModel for a partial view that displays the logged in users DisplayName. This Partial view is in the main layout so it will be on every page. Sounds simple I know; however for the life of me I can’t figure out the best way to get the data to the View. How do I persist this view?
Share
The best way would probably to use child actions along with the Html.Action helper.
So as always in ASP.NET MVC you start with a view model which will represent the information you are willing to manipulate/display in a view:
then a controller:
a corresponding partial view:
then go ahead in your _Layout and include this action:
Obviously the next improvement to this code would be to avoid hitting the database on each request but storing this information somewhere once the user logs in as it will be present on all pages. Excellent places for this are for example the userData part of the encrypted authentication cookie (if you are using FormsAuthentication of course), Session, …