I am a beginner in ASP.Net MVC web development. And I want to know how can I access extra property added in my ApplicationUser model in layout file?
As of now if I have to access a property in the razor file I add @model myModel
at top of Razor file and then I am able to access the property by @Model.MyProperty
.
Suppose I added FirstName
property in my Application User
like below:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
}
Now in my Login Partial I have below Code.
@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
Suppose I also want to display FirstName instead of User.Identity.GetUserName()
how can I do so.
Below is the RegisterViewModel which user will see during registration.
public class RegisterViewModel
{
// rest property removed for brevity
[Required]
public string FirstName { get; set; }
}
Now when a view is returned, the RenderBody() is the place where the actual CSHTML file related to that controller action gets rendered. But in my scenario I need to access the property of applicationUser in layout file which is common for all. Please guide me.
Can I do something like: User.Identity.GetFirstName or something along these lines?