Daniel K Rudolf_mag
Daniel K Rudolf_mag

Reputation: 287

Display name instead editor field in MVC4

I have in my edit form displayed: Username, TimeZone, Customer...

I don't whant to be able to edit username, just display his name.

This code I use in View:

    <label>Username </label> 
    <div class="editor-field">
        @Html.EditorFor(model => model.username)
    </div>

So what to put instead EditorFor, that will display username (just for reading, not for editing).

Upvotes: 0

Views: 309

Answers (2)

Bhushan Shah
Bhushan Shah

Reputation: 1038

Why not just use @Html.DisplayFor instead? This will just display the username as a label. Or, if you wish to use @Html.EditorFor or @Html.EditorForModel, you can create a custom editor template for your username property, and in the editor template, just display the content instead of enabling editing.

Also, I would recomment you exclude this property during model binding by using [Bind(Exclude="username")] with your model parameter in your POST action method, to protect from injection attacks. More about this here.

Upvotes: 1

Daniel K Rudolf_mag
Daniel K Rudolf_mag

Reputation: 287

find solution:

@Html.TextBoxFor(model => model.username, new {disabled = "disabled", @readonly = "readonly" })

Upvotes: 0

Related Questions