0

我想为出现在我的应用程序的许多页面上的特定类型的下拉菜单创建一个 HtmlHelper 函数。我基本上只是想在现有的 DropDownList 函数周围添加一些装饰,但它正在被编码。这是我的扩展方法:

    <Extension()> _
    Public Function Year(ByVal HtmlHelper As System.Web.Mvc.HtmlHelper, ByVal name As String) As String

        Return _
            <label>
                Year:
                <%= HtmlHelper.DropDownList(name) %>
            </label>.ToString

    End Function

这将返回以下内容:

<label>Year:&lt;select id="year" name="year"&gt;&lt;option value="2007"&gt;2007&lt;/option&gt;&lt;option value="2008"&gt;2008&lt;/option&gt;&lt;option selected="selected" value="2009"&gt;2009&lt;/option&gt;&lt;/select&gt;</label>

而不是我想要的:

<label>Year:<select id="year" name="year"><option value="2007">2007</option><option value="2008">2008</option><option selected="selected" value="2009">2009</option></select></label>

换句话说,DropDownList 在将字符串放入标签之前进行了 HTML 编码。我可以这样做:

    <Extension()> _
    Public Function Year(ByVal HtmlHelper As System.Web.Mvc.HtmlHelper, ByVal name As String) As String

        Return "<label>Year:" & HtmlHelper.DropDownList(name) & "</label>"

    End Function

但我宁愿使用 VB 的内联 XML。如何使 DropDownList 的结果不被编码?

4

1 回答 1

1

首先,您真的希望标签围绕下拉而不是在它之前吗?

Then, I gather that HtmlHelper.DropDownList(name) returns a string? Then of course, it should be encoded. If you want XML, then you should use something like XElement.Parse(HtmlHelper.DropDownList(name)) instead.

I'm not sure of the syntax to do that inside an XML literal, but this is small enough you could just use straight LINQ to XML:

Dim result as XElement = New XElement("label", _
   XElement.Parse( HtmlHelper.DropDownList(name)))
Return result.ToString()
于 2009-08-13T18:30:46.150 回答