I’m trying to write an extension for DropDownListFor:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool enabled)
{
return htmlHelper.DropDownListFor(expression, selectList, null /* optionLabel */, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
What I want to achieve is if enabled is false no change but if enabled is true I want to add @disabled="disabled" to the html attributes before giving them to AnonymousObjectToHtmlAttributes.
Any ideas on how to do this?
Simple!
HtmlHelper.AnonymousObjectToHtmlAttributesreturnsRouteValueDictionary. You can add value to that dictionary, you do not need to add property to anonymous object.