mvc @html.dropdownlistfor,asp.net mvc - Html.DropDownListFor() with custom parameter - Stack Overflo...

You can build your CustomDropdownListFor with the help of a custom helper method as shown below:

Custom Helper Method:

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(this HtmlHelper htmlHelper, Expression> expression, IEnumerable list, string selectedValue, string optionLabel, object htmlAttributes = null)

{

if (expression == null)

{

throw new ArgumentNullException("expression");

}

ModelMetadata metadata = ModelMetadata.FromLambdaExpression(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 list, string selectedValue, IDictionary 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.Append("" + optionLabel + "");

foreach (var item in list)

{

if (item.SelectedValue == "selected" && item.Disabled == "disabled")

options.Append("" + item.Text + "");

else if (item.SelectedValue != "selected" && item.Disabled == "disabled")

options.Append("" + item.Text + "");

else if (item.SelectedValue == "selected" && item.Disabled != "disabled")

options.Append("" + item.Text + "");

else

options.Append("" + item.Text + "");

}

dropdown.InnerHtml = options.ToString();

return MvcHtmlString.Create(dropdown.ToString(TagRenderMode.Normal));

}

}

View (Razor):

@Html.CustomDropdownListFor(m => m.PersonId, ViewBag.PersonData as List, null, "---- Select ----",

new { name = "personId", id = "personId"})

@Html.ValidationMessageFor(m => m.PersonId, null , new { @class = "ValidationErrors" })

Hope this helps...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值