Html.DropDownListFor()自定义参数 [英] Html.DropDownListFor() with custom parameter
本文介绍了Html.DropDownListFor()自定义参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在HTML辅助添加扩展方法生成选择和选项,这样
I want to add extension method in HTML helpers for generating select and options like this
<select id="Country" name="Country">
<option data-domain="AN" value="1">Andorra</option>
<option data-domain="UI" value="2">United Arab Emirates</option>
<option data-domain="AF" value="3">Afghanistan</option>
选项有一个数据域的属性,我可以用它像这样
the options have a data-domain attribute, and I can use it like this
@Html.DropDownListFor(m => m.Country, Model.CountryList)
在 Model.CountryList
是国家valiables的arrayh
the Model.CountryList
is an arrayh of Country valiables
class Country
{
public String Text { get; set; }
public String Value { get; set; }
public String Domain { get; set; }
}
请谁能给出一个解决方案。
please can anyone give a solution
推荐答案
您可以使用自己的自定义CustomDropdownListFor定制的辅助方法的帮助下象下面这样:
You can use your own custom CustomDropdownListFor with the help of custom helper method like below:
自定义helper方法:
public static class CustomHelpers
{
public class CustomSelectItem : SelectListItem
{
public string Class { get; set; }
public string Disabled { get; set; }
public string SelectedValue { get; set; }
}
public static MvcHtmlString CustomDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<CustomSelectItem> list, string selectedValue, string optionLabel, object htmlAttributes = null)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
string name = ExpressionHelper.GetExpressionText((LambdaExpression)expression);
return CustomDropdownList(htmlHelper, metadata, name, optionLabel, list, selectedValue, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
private static MvcHtmlString CustomDropdownList(this HtmlHelper htmlHelper, ModelMetadata metadata, string name, string optionLabel, IEnumerable<CustomSelectItem> list, string selectedValue, IDictionary<string, object> htmlAttributes)
{
string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
if (String.IsNullOrEmpty(fullName))
{
throw new ArgumentException("name");
}
TagBuilder dropdown = new TagBuilder("select");
dropdown.Attributes.Add("name", fullName);
dropdown.MergeAttribute("data-val", "true");
dropdown.MergeAttribute("data-val-required", "Mandatory field.");
dropdown.MergeAttribute("data-val-number", "The field must be a number.");
dropdown.MergeAttributes(htmlAttributes); //dropdown.MergeAttributes(new RouteValueDictionary(htmlAttributes));
dropdown.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
StringBuilder options = new StringBuilder();
// Make optionLabel the first item that gets rendered.
if (optionLabel != null)
options = options.Append("<option value='" + String.Empty + "'>" + optionLabel + "</option>");
foreach (var item in list)
{
if (item.SelectedValue == "selected" && item.Disabled == "disabled")
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "' selected='" + item.SelectedValue + "' disabled='" + item.Disabled + "'>" + item.Text + "</option>");
else if (item.SelectedValue != "selected" && item.Disabled == "disabled")
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "' disabled='" + item.Disabled + "'>" + item.Text + "</option>");
else if (item.SelectedValue == "selected" && item.Disabled != "disabled")
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "' selected='" + item.SelectedValue + "'>" + item.Text + "</option>");
else
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "'>" + item.Text + "</option>");
}
dropdown.InnerHtml = options.ToString();
return MvcHtmlString.Create(dropdown.ToString(TagRenderMode.Normal));
}
}
剃刀:
@Html.CustomDropdownListFor(m => m.PersonId, ViewBag.PersonData as List<CustomHelpers.CustomSelectItem>, null, "---- Select ----",
new { name = "personId", id = "personId"})
@Html.ValidationMessageFor(m => m.PersonId, null , new { @class = "ValidationErrors" })
这篇关于Html.DropDownListFor()自定义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文