我从数据库填充下拉列表,我想通过设置它们的“类”属性以红色显示某些元素。所以,我尝试了很多方法,后来我为 Dropdownlist 创建了一个自定义 HTML Helper。但这没有任何意义,虽然它似乎是添加类属性,但这个参数不能从 Controller 传递给 Razor View。你能帮忙吗?
MyHelper.cs:
public static MvcHtmlString Custom_DropdownList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> list, object htmlAttributes)
{
TagBuilder dropdown = new TagBuilder("select");
dropdown.Attributes.Add("name", name);
dropdown.Attributes.Add("id", name);
StringBuilder options = new StringBuilder();
foreach (var item in list)
{
options = options.Append("<option value='" + item.Value + "'>" + item.Text + "</option>");
}
dropdown.InnerHtml = options.ToString();
dropdown.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return MvcHtmlString.Create(dropdown.ToString(TagRenderMode.Normal));
}
控制器:
private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
var meetingsQuery = repository.Meetings
.Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
(m, c) => new
{
CityID = c.CityID,
CityName = c.CityName,
MeetingDate = m.MeetingStartDate
}
)
.OrderBy(x => x.CityID)
.AsEnumerable()
.Select(
i => new
{
CityID = i.CityID,
Name = string.Format(
"{0} ({1:dd MMMM yyyy})",
i.CityName, i.MeetingDate),
Expired = i.MeetingDate < DateTime.UtcNow
}
).ToList();
var selectItems = new List<SelectListItem>(meetingsQuery.Count);
foreach (var record in meetingsQuery)
{
var item = new SelectListItem
{
Text = record.Name,
Value = record.Name
};
if (record.Expired)
{
item.Attributes.Add("class", "disabled"); //!!! Problem on this line
}
selectItems.Add(item);
}
ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "Name", selectedMeetings);
}
但是在应用此方法后,我收到一个错误“'System.Web.Mvc.SelectListItem' 不包含'Attributes' 的定义并且没有扩展方法'Attributes' 接受类型的第一个参数......”。所以,我认为我需要使用另一个属性或助手将类属性分配给自定义 racords(在“if (record.Expired)”行过滤记录没有问题)。
看法:
@Html.Custom_DropdownList("MeetingId", ViewData["MeetingId"] as SelectList, new { id = "meetingId"})
你能澄清我如何提供这个吗?提前致谢。
这是具有 Class 和 Disabled 属性的修改后的代码:
更新代码(用于 MyDropdownListFor):
自定义 HTML 助手类:
public static class MyHelpers
{
public class MySelectItem : SelectListItem
{
public string Class { get; set; }
public string Disabled { get; set; }
}
public static MvcHtmlString MyDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<MySelectItem> list, string optionLabel, object htmlAttributes)
{
return MyDropdownList(htmlHelper, ExpressionHelper.GetExpressionText(expression), list, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString MyDropdownList(this HtmlHelper htmlHelper, string name, IEnumerable<MySelectItem> list, string optionLabel, IDictionary<string, object> htmlAttributes)
{
TagBuilder dropdown = new TagBuilder("select");
dropdown.Attributes.Add("name", name);
dropdown.Attributes.Add("id", name);
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.Disabled == "disabled")
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "' disabled='" + item.Disabled + "'>" + item.Text + "</option>");
else
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "'>" + item.Text + "</option>");
}
dropdown.InnerHtml = options.ToString();
dropdown.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return MvcHtmlString.Create(dropdown.ToString(TagRenderMode.Normal));
}
}
控制器:
private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
var meetingsQuery = repository.Meetings
.Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
(m, c) => new
{
CityID = c.CityID,
CityName = c.CityName,
MeetingDate = m.MeetingStartDate
}
)
.OrderBy(x => x.CityID)
.AsEnumerable()
.Select(
i => new
{
Value = i.CityID.ToString(),
DisplayValue = string.Format(
"{0} ({1:dd MMMM yyyy})",
i.CityName, i.MeetingDate),
Expired = i.MeetingDate < DateTime.UtcNow
}
).ToList();
var selectItems = new List<MyHelpers.MySelectItem>(meetingsQuery.Count);
foreach (var record in meetingsQuery)
{
var item = new MyHelpers.MySelectItem
{
Text = record.DisplayValue,
Value = record.Value
};
if (record.Expired)
{
item.Class = "disabled";
item.Disabled = "disabled";
}
selectItems.Add(item);
}
ViewBag.MeetingData = selectItems;
}
看法:
<label>Meeting</label>
@Html.MyDropdownListFor(m => m.MeetingId, ViewBag.MeetingData as List<MyHelpers.MySelectItem>, "---- Select ----",
new { name = "meetingId", id = "meetingId"})