@Html.TextBoxFor(
model => model.SomeProperty,
new { data_url = "http://www.google.com" }
)
отображается как
<input id="SomeProperty" data-url="http://www.google.com">
У меня есть несколько пользовательских расширений, похожих на это
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, bool disabled)
{
var htmlAttrs = new RouteValueDictionary(htmlAttributes);
return TextBoxFor<TModel, TProperty>(htmlHelper, expression, htmlAttrs, disabled);
}
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes, bool disabled)
{
if (disabled)
{
htmlAttributes["disabled"] = true;
}
return htmlHelper.TextBoxFor(expression, htmlAttributes);
}
Первая перегрузка, где используется object htmlAttributes
htmlAttributes
, вызывает вторую перегрузку, где htmlAttributes
преобразуется в IDictionary<string, object> htmlAttributes
, а собственный TextBoxFor вызывается с IDictionary<string, object>
. Это значит
@Html.TextBoxFor(
model => model.SomeProperty,
new { data_url = "http://www.google.com" }, model.IsSomePropertyDisabled
)
отображается как
<input id="SomeProperty" data_url="http://www.google.com">
Примечание data_url вместо data-url .
По существу,
public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes);
достаточно умен, чтобы знать, что data_wh независимо должно быть отображено как data-независимо , тогда как
public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, IDictionary<string, object> htmlAttributes);
, кажется, не применяет эту логику (или мы можем сказать, что недостаточно умен). Есть ли способ заставить его применять ту же логику?
В качестве обходного пути я мог бы не использовать эти перегрузки, когда мне действительно нужны атрибуты данных, и иметь подробные представления, когда должен применяться атрибут disable. Любой другой путь?