0

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.

1 Answer 1

1

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());      
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.