Display a readonly field in ASP.NET Core

Try this.

<input asp-for="Name" class="form-control" readonly="@(true)">
<input asp-for="Name" class="form-control" readonly="@(false)">

This render:

<input class="form-control" type="text" id="Name" name="Name" value="Tom" readonly="readonly">
<input class="form-control" type="text" id="Name" name="Name" value="Tom">

I realized it from here: https://github.com/aspnet/Mvc/issues/7333#issuecomment-363504164


Output it as HTML with Razor syntax

https://docs.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...