Question

I wrote an extension method :

    public static string XDropDown(this HtmlHelper helper,string name, string optionLabel,object selectedValue)
    { 
        StringBuilder b = new StringBuilder();

        b.AppendFormat("<select name='{0}' id='{0}'>", name);

        b.Append("</select>");

        return b.ToString();      
    }

The rendered version :

&lt;select name=&#39;CCName&#39; id=&#39;CCName&#39;&gt;&lt;option value=&amp;quot;BT&amp;quot;&gt;Bhutan&lt;/option&gt;&lt;/select&gt;

and I am using it from a partial view, it isn't rendered as it's expected, I know that I can use Tag builders also, but eager to know weather if this could work somehow or not.

Was it helpful?

Solution

Use the MvcHtmlString as the return type like so:

public static MvcHtmlString XDropDown(
        this HtmlHelper helper,
        string name, 
        string optionLabel,
        object selectedValue)
{ 
    StringBuilder b = new StringBuilder();
    b.AppendFormat("<select name='{0}' id='{0}'>", name);
    b.Append("</select>");
    return MvcHtmlString.Create(b.ToString());      
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top