本文介绍了ASP MVC Razor 对输入占位符中的特殊字符进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
型号:
[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" })
它将呈现如下:
贵公司的名称
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&#39;s name" type="text" value="">
应该是这样的:
贵公司名称
为什么文本无法正确呈现,我该如何防止?
Why is the text does not render correctly and how can I prevent this?
我已经试过了:
@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" })
推荐答案
我认为这篇文章会对你有所帮助:
I think this post will help you:
我认为还有其他方法可以获得这种行为,但这是使用 TextBox 的一种选择:
I think there are other ways to get this behaviour, but this is one option of using the TextBox:
@Html.TextBox("CompanyName", HttpUtility.HtmlEncode("Your company's name"))
还有 HttpUtility.HtmlDecode
,它可能有助于我们的保存操作.
There is also HttpUtility.HtmlDecode
, which might help with our save action.
更新
如果将 HttpUtility.HtmlDecode
包裹在占位符周围:
if you wrap HttpUtility.HtmlDecode
around your place holder:
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input",
@placeholder = HttpUtility.HtmlDecode(Html.DisplayNameFor(x => x.CompanyName).ToHtmlString()),
@id = "companyname" })
占位符返回为:placeholder="贵公司名称"
the placeholder returns as: placeholder="Your company's name"
这篇关于ASP MVC Razor 对输入占位符中的特殊字符进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!