abcdef
abcdef

Reputation: 33

Get current username in view MVC 4

i'm trying to get the current username logged in in a view but i can't do it. I have this code:

<div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Name, System.Environment.GetEnvironmentVariable(User.Identity.Name))
    </div>

But it doesn't work. I want to when the user goes to the form it displays his username automatically in a way he can't edit. Everytime i try something i search or it doesn't appear the username or it sends a blank space instead of the username. Can someone help me? Or should i declare in the class or controller that the variable Name is the current username (if so how can i do it?)? I'm using MVC 4 with EF 4.5 and SimpleMembership

Upvotes: 1

Views: 6554

Answers (2)

Ibrahim Khan
Ibrahim Khan

Reputation: 20750

You can do it like following.

@Html.TextBox("Name", User.Identity.Name)

Upvotes: 2

Webbo
Webbo

Reputation: 82

This works for us in an Umbraco Razor view. I think it will be the same in plain MVC outside Umbraco

You are logged in as @Context.User.Identity.Name)

Upvotes: 1

Related Questions