gpt4 book ai didi

razor - ASP MVC Razor 在输入占位符中编码特殊字符

转载 作者:行者123 更新时间:2023-12-03 11:47:03 24 4
gpt4 key购买 nike

这是我的代码:

模型:

[Required]
[DataType(DataType.Text)]
[Display(Name = "Your company's name")]
public string CompanyName { get; set; }

看法:
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", @placeholder = @Html.DisplayNameFor(m => m.CompanyName), @id = "companyname" })

它将呈现如下:

Your company's name



html输出:
<input class="account-input" data-val="true" data-val-required="The Your company's name field is required." id="companyname" name="CompanyName" placeholder="Your company&amp;#39;s name" type="text" value="">

它应该是这样的:

Your company's name



为什么文本无法正确呈现,我该如何防止这种情况?

我已经尝试过了:
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", @placeholder = @Html.Raw(@Html.DisplayNameFor(m => m.CompanyName)), @id = "companyname" })

还有这个
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", @placeholder = @Html.Encode(@Html.DisplayNameFor(m => m.CompanyName)), @id = "companyname" })

最佳答案

我认为这篇文章会帮助你:

HTML encode decode c# MVC4

我认为还有其他方法可以获得这种行为,但这是使用 TextBox 的一种选择:

@Html.TextBox("CompanyName", HttpUtility.HtmlEncode("Your company&#39;s name"))

还有 HttpUtility.HtmlDecode ,这可能有助于我们的保存操作。

更新

如果你包装 HttpUtility.HtmlDecode在您的占位符周围:
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", 
@placeholder = HttpUtility.HtmlDecode(Html.DisplayNameFor(x => x.CompanyName).ToHtmlString()),
@id = "companyname" })

占位符返回为:
placeholder="贵公司名称"

关于razor - ASP MVC Razor 在输入占位符中编码特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15246439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com