Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp MVC 4 creating custom html helper method similar to Html.BeginForm

I have the following html:

<div data-bind="stopBindings">

    <div data-viewId="languageList" data-bind="with: viewModel">
       <table>
              <tr>
                   <td ><label for="availableLanguages">Available Languages:</label></td>
              </tr>
       <table>
    </div>

</div>

I want to make a custom html helper and use it like this (similar to Html.BeginForm)

@Html.BeginView()
{
    <table>
        <tr>
            <td ><label for="availableLanguages">Available Languages:</label></td>
        </tr>
    </table>
}

I started making my helper method

public static class BeginViewHelper
    {
        public static MvcHtmlString BeginView(this HtmlHelper helper, string viewId)
        {

            var parentDiv = new TagBuilder("div");
            parentDiv.MergeAttribute("data-bind", "preventBinding: true");
            return new MvcHtmlString();
        }

    }

I read how to make basic html helper but the examples I saw does not give me information how to make it in my case. I am very new to asp mvc and every help will be greatly appreciated.

UPDATE 2:

Obviously I am missing something. I am calling this in my view:

@Html.BeginView()
{
    <table>
        <tr>
            <td ><label >test</label></td>
        </tr>
    </table>
}

Everything seems fine it even has intellisense. But the output in the browser is the following:

Omega.UI.WebMvc.Helpers.BeginViewHelper+MyView { 


test

 } 

This is my helper method:

namespace Omega.UI.WebMvc.Helpers
{
    public static class BeginViewHelper
    {
        public static IDisposable BeginView(this HtmlHelper helper)
        {
            helper.ViewContext.Writer.Write("<div data-bind=\"preventBinding: true\">");
            helper.ViewContext.Writer.Write("<div data-viewId=\"test\">");

            return new MyView(helper);
        }

        class MyView : IDisposable
        {
            private HtmlHelper _helper;

            public MyView(HtmlHelper helper)
            {
                this._helper = helper;
            }

            public void Dispose()
            {
                this._helper.ViewContext.Writer.Write("</div>");
                this._helper.ViewContext.Writer.Write("</div>");
            }
        }
    }
}

and I have registered the namespace in ~/Views/web.config

 <add namespace="Omega.UI.WebMvc.Helpers" />
like image 288
Mdb Avatar asked Jan 09 '13 13:01

Mdb


1 Answers

You can't return MvcHtmlString. Instead of that you should write html to the writer and return class that implement IDisposable and during invoking Dispose will write closing part of your HTML.

public static class BeginViewHelper
{
    public static IDisposable BeginView(this HtmlHelper helper, string viewId)
    {
        helper.ViewContext.Writer.Write(string.Format("<div id='{0}'>", viewId));

        return new MyView(helper);
    }

    class MyView : IDisposable
    {
        private HtmlHelper helper;

        public MyView(HtmlHelper helper)
        {
            this.helper = helper;
        }

        public void Dispose()
        {
            this.helper.ViewContext.Writer.Write("</div>");
        }
    }
}

If you have more complex structure you can try to use TagBuilder:

TagBuilder tb = new TagBuilder("div");
helper.ViewContext.Writer.Write(tb.ToString(TagRenderMode.StartTag));
like image 63
Sławomir Rosiek Avatar answered Oct 27 '22 01:10

Sławomir Rosiek