我有一个自定义 AjaxHelper 呈现启用 ajax 的下拉列表。代码如下。帮助程序运行良好,但是当验证失败时它不会添加 input-validation-error 类,因此它不会以我想要的方式与其他表单元素一起突出显示。在 Html 助手中,我可以合并 HtmlHelper.GetUnobtrusiveValidationAttributes() 给出的属性。但是我如何在 AjaxHelper 中做到这一点?
public static MvcHtmlString DropDownList(this AjaxHelper ajaxHelper, string name,
IEnumerable<SelectListItem> list, string optionLabel,
AjaxOptions ajaxOptions, object htmlAttributes)
{
var tag = new TagBuilder("select");
StringBuilder options = new StringBuilder();
tag.MergeAttribute("name", name);
tag.MergeAttribute("id", name);
TagBuilder option;
if (optionLabel != null)
{
option = new TagBuilder("option");
option.MergeAttribute("value", string.Empty);
option.SetInnerText(optionLabel);
options.Append(option.ToString());
}
foreach (SelectListItem item in list)
{
option = new TagBuilder("option");
option.MergeAttribute("value", item.Value);
if (item.Selected)
{
option.MergeAttribute("selected", "selected");
}
option.SetInnerText(item.Text);
options.Append(option.ToString());
}
tag.InnerHtml = options.ToString();
tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}