做ASP.NET MVC的辅助方法,如Html.DropDownList()带code输出HTML? [英] Do ASP.NET MVC helper methods like Html.DropDownList() encode the output HTML?
问题描述
我只是想知道,如果我担心编码,当我使用HTML佣工像 Html.DropDownList()
。
I am just wondering if I have to worry about encoding the values that get output when I use HTML helpers like Html.DropDownList()
.
如果是这样,我怎么带code呢?这很容易做到,如果我手动降低建筑下降 - 只是包装,每个值 Html.En code()
。但是,我不知道如何使用HTML佣工时,要做到这一点。
If so, how do I encode them? It's easy to do if I were building the drop down manually -- just wrap each value with "Html.Encode()
". However, I don't know how to do this when using HTML helpers.
推荐答案
它看起来像值均设有codeD自动,所以没有理由去自己做。下面是从实际的ASP.NET MVC 1.0源$ C $ C片段,您可以从codePLEX(以SelectExtensions.cs)下载:
It looks like the values are encoded automatically, so there's no reason to do it yourself. Here's a snippet from the actual ASP.NET MVC 1.0 source code that you can download from codeplex (in SelectExtensions.cs):
private static string ListItemToOption(SelectListItem item) {
TagBuilder builder = new TagBuilder("option") {
InnerHtml = HttpUtility.HtmlEncode(item.Text)
};
if (item.Value != null) {
builder.Attributes["value"] = item.Value;
}
if (item.Selected) {
builder.Attributes["selected"] = "selected";
}
return builder.ToString(TagRenderMode.Normal);
}
这篇关于做ASP.NET MVC的辅助方法,如Html.DropDownList()带code输出HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!