Changing Html.DisplayFor boolean checkbox MVC

前端 未结 4 1091
生来不讨喜
生来不讨喜 2021-02-12 07:30

I have a boolean property IsActive. In the view is a list of objects with their properties (including IsActive). But in the list the IsActive is a non-editable checkbox since it

4条回答
  •  攒了一身酷
    2021-02-12 07:51

    I'd like to complement nightshifted's solution so that it works well with localized string as well. Also, using @Html.Encode() strings with special characters are encoded wrong. For example "Kyllä" (Finnish for Yes) gets encoded badly. That is why there is no @Html.Encode() at all in my solution.

    This solution assumes that you are using localization with SharedResources approach (refer ASP.NET Core Localization with help of SharedResources)

    @using Microsoft.AspNetCore.Mvc.Localization
    @model bool
    @inject IHtmlLocalizer L
    
    @if (Model)
    {
        @L["Yes"]
    }
    else
    {
        @L["No"]
    }
    

提交回复
热议问题