The code below will create a CustomTextBoxFor helper by extending TextBoxFor. This allows you to take full advantage of MVC's validation conventions as well leaving open the htmlAttributes parameter so more attributes can be appended as needed.
public static MvcHtmlString CustomTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, string customData)
{
return htmlHelper.CustomTextBoxFor(expression, customData, (IDictionary<string, object>)null);
}
public static MvcHtmlString CustomTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, string customData, object htmlAttributes)
{
IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
attributes.Add("data-custom", customData);
return htmlHelper.TextBoxFor(expression, new { data_custom = "customData" });
}
Usage:
@Html.CustomTextBoxFor(model => model.MyProperty, Model.CustomData)
@Html.CustomTextBoxFor(model => model.MyProperty, Model.CustomData, new { @class="pretty"})