Search code examples
c#asp.net-coreasp.net-core-tag-helpers

Display a readonly field in ASP.NET Core


I am new in ASP.NET Core and would like to ask about displaying a field in w a view. I have a

class Person { 
    public string Name {get; set;} 
    public string Nickname {get; set;} 
}

Typically, in the EditPerson View, I do

<input asp-for="Name" class="form-control" />
<input asp-for="Nickname" class="form-control" />

to display the fields.

But in my EditPerson View I need to display Name as readonly, and Nickname as editable field.

The Name should not be in a textbox(even readonly), but rather in a label or div...

Is there a standard approach to display such fields?

I don't want that the field to be updated back to the model or DB, I need only reading the field.


Solution

  • Output it as HTML with Razor syntax

    https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor

    @Model.Name
    <input asp-for="Nickname" class="form-control" />
    

    Note - this will need styling appropriately, or wrapping in <span> tag etc...