1

I have problem with custom html helper. I try to build a helper, using TagBuilder but I'm unable to close it.

Here is my code:

 public static HtmlString CustomHelper(this HtmlHelper htmlHelper,
        string id)
    {
        var contentDiv = new TagBuilder("div");
        contentDiv.MergeAttribute("style", "display:inline-block");

        var input = new TagBuilder("input");
        input.AddCssClass("forDD");
        input.MergeAttribute("type", "hidden");
        input.MergeAttribute("id", id);
        input.MergeAttribute("value", "Cat");

        contentDiv.InnerHtml += input;


        return new HtmlString(contentDiv.ToString(TagRenderMode.EndTag));
    } 

But the result of it looks like:

enter image description here

Something is wrong but I can't find out what, I'm missing it. Even closing input tag is wrong. I have checked version of dlls and have tried with MvcHtmlString ect. Also the TagRenderMode doesn't work at all.

Thanks for the help.

Best regards.

krypru
  • 1,202
  • 3
  • 17
  • 28

1 Answers1

2

Try this, It worked for me. No need to use TagRenderMode.EndTag.

public  HtmlString CustomHelper(  string id)
        {
            var contentDiv = new TagBuilder("div");
            contentDiv.MergeAttribute("style", "display:inline-block"); 
            var input = new TagBuilder("input");
            input.AddCssClass("forDD");
            input.MergeAttribute("type", "hidden");
            input.MergeAttribute("id", id);
            input.MergeAttribute("value", "Cat"); 
            contentDiv.InnerHtml += input;
            return new HtmlString(contentDiv.ToString());
        } 

Any my result is

<div style="display:inline-block"><input class="forDD" id="3" type="hidden" value="Cat"></input></div>
DeepakJ
  • 380
  • 4
  • 14
  • Thanks for the reply but the Frebin Francis's answer fits for me more because I wanted to self close tag. – krypru Feb 25 '15 at 12:10