Monday, 5 February 2018

How to create custom helper in mvc

Here, i will create own control for TextBox and label-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CustomControlInMVC
{
    public static class htmlControl
    {
        public static MvcHtmlString myTextBox(this HtmlHelper helper, string name)
        {
            string str = "<input type=\"text\" name="+ name + " id="+ name + " />";
            return new MvcHtmlString(str);
        }

        public static MvcHtmlString myLabel(this HtmlHelper helper, string name, string content)
        {
            string str = string.Format("<lable class='color:red' name=" + name + " id=" + name + ">{0}</label>", content);
            return new MvcHtmlString(str);
        }
    }
}

using controls like-

 <div>
        @Html.myTextBox("txtMyTextBox")
        @Html.myLabel("lbl", "This is custom label")
    </div>


No comments:

Post a Comment