Reputation: 121
I have a custom control used in web application,I want to create html helper for an MVC helper using the logic of this custom control
Here's the code for the web application that I want to convert :
[ToolboxData("<{0}:CustDropDownList runat=server></{0}:CustDropDownList>")]
public class CustDropDownList : System.Web.UI.WebControls.DropDownList
{
protected override void Render(HtmlTextWriter writer)
{
var strDivAttributes = this.Enabled ? "select-box" : "select-box disabled";
writer.Write("<div id=\"{0}Div\" class=\"{1}\">", this.ClientID, strDivAttributes);
base.Render(writer);
writer.Write("</div>");
}
}
Upvotes: 0
Views: 846
Reputation: 126
You can add an extension method to the HtmlHelper class like this:
public static MvcHtmlString CustDropDownList(this HtmlHelper htmlHelper)
{
// ... your custom logic goes here
return new MvcHtmlString.Create("<div>some HTML...</div>");
}
You can then call it from a view like so:
@Html.CustDropDownList()
EDIT If you want to use this for a model property you'll need a something like this:
public static MvcHtmlString CustDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
// ... your custom logic goes here
return new MvcHtmlString.Create("<div>some HTML...</div>");
}
Which can then be used like this:
@Html.CustDropDownListFor(m => m.SomeProperty)
Upvotes: 2