简体   繁体   中英

Is it possible to not encode the Label tag value rendered in ASP.NET Core HtmlHelper?

I asked a similar question a while back: Is it possible to format Display attribute's Name property value of MVC Model class?

I am wondering would it be possible to not apply encoding in the ASP.NET Core HtmlHelper.Label tag?

What I have now is:

<label asp-for="CorporationName" class="control-label"></label>

I understand it will render the html.

Like so:

<label class="control-label" for="CorporationName">Corporation / <br/>Entreprise</label>

with the text value defined in this model class:

[DataType(DataType.Text)]
[Display(Name = "Corporation / <br/> Enterprise")]
public string Corporation { get; set; }

So I am wondering if it is possible to apply something similar to @Html.Raw to the asp-for attribute value in the label tag above so it will show as:

Corporation /
Enterprise

Or I need to write my own label class so it will do the encoding?

Thank you.

I think your requirement can be known as, you want to use tag helper and display [Display(Name = "Corporation / Enterprise")] to display as Corporation / </br> Enterprise .

Basically, you are trying to change the html content appearance, and you want to realize it via taghelper, but actually there's no such feature, you may try to create a custom taghelper like this answer . But I think it's too complex.

In my humble opinion, you can modify the html content by js. But the easiest way to add linebreaker into html content is </br> , so my test code looks like below, it worked for me:

<div>
    <label id="CorporationLabor" asp-for="Corporation" class="control-label"></label>
    <input asp-for="Corporation" class="form-control" />
</div>
@section Scripts{
    <script>
        $(function(){
            $("#CorporationLabor").html("Corporation / </br> Enterprise!!!");
        });
    </script>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM