当它回发时,我收到以下错误:
具有键'ClosingDateDay'的ViewData项的类型为'System.Int32',但必须是'IEnumerable'类型。有什么想法吗?
这是我的控制器:
CompetitionEditViewModel viewModel = new CompetitionEditViewModel
{
ClosingDate = competition.CloseDate,
Description = competition.Description,
DescriptionHeading = competition.DescriptionHeading,
ImageAssetId = competition.ImageAssetId,
IsActive = competition.IsActive,
MainHeading = competition.MainHeading,
TermsAndConditions = competition.TermsAndConditions,
UrlSlug = competition.UrlSlug
};
viewModel.ClosingDateMonthOptions = new List<SelectListItem>();
for (int i = 1; i <= 12; i++)
{
string monthName = new DateTime(2000, i, 1).ToString("MMMM");
((List<SelectListItem>)viewModel.ClosingDateMonthOptions).Add(new SelectListItem { Text = monthName, Value = i.ToString() });
}
viewModel.ClosingDateDayOptions = new List<SelectListItem>();
for (int i = 1; i <= 31; i++)
{
((List<SelectListItem>)viewModel.ClosingDateDayOptions).Add(new SelectListItem { Text = i.ToString().PadLeft(2, '0'), Value = i.ToString() });
}
viewModel.ClosingDateYearOptions = new List<SelectListItem>();
for (int i = DateTime.Now.Year; i <= DateTime.Now.Year + 3; i++)
{
((List<SelectListItem>)viewModel.ClosingDateYearOptions).Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
}
继承了我的观点:
@Html.Uber().LabelFor(x => x.ClosingDateDay, new { @class = "access" })
@Html.DropDownListFor(x => x.ClosingDateDay, Model.ClosingDateDayOptions, Model.ClosingDateDay)
@Html.Uber().LabelFor(x => x.ClosingDateMonth, new { @class = "access" })
@Html.DropDownListFor(x => x.ClosingDateMonth, Model.ClosingDateMonthOptions, Model.ClosingDateMonth)
@Html.Uber().LabelFor(x => x.ClosingDateYear, new { @class = "access" })
@Html.DropDownListFor(x => x.ClosingDateYear, Model.ClosingDateYearOptions, Model.ClosingDateYear)
答案 0 :(得分:9)
构造SelectListItem类时,请为最初选择的项目将Selected
属性设置为true。
答案 1 :(得分:1)
我在我的一个项目中所做的并且有点有用,就是为了增加2个重载
接受DropDownListFor
的{{1}}。
selectedValue
所以在Views中,我可以将一个字符串作为selectedValue传递给namespace MyMvcApplication.Helpers
{
public static class ExtensionMethods
{
public static MvcHtmlString DropDownListFor<TModel, TProperty>
(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression,
string selectedValue,
IEnumerable<SelectListItem> selectList,
string optionLabel,
object htmlAttributes)
{
if (string.IsNullOrEmpty(selectedValue))
selectedValue = string.Empty;
if (selectList != null)
{
foreach (SelectListItem sli in selectList)
{
if (sli.Value.ToLower().Trim() == selectedValue.ToLower().Trim())
{
sli.Selected = true;
break;
}
}
}
else
{
selectList = new List<SelectListItem>()
{ new SelectListItem()
{ Text = "", Value = "", Selected = true }
};
}
return helper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}
public static MvcHtmlString DropDownListFor<TModel, TProperty>
(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression,
string selectedValue,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, object> htmlAttributes)
{
if (string.IsNullOrEmpty(selectedValue))
selectedValue = string.Empty;
if (selectList != null)
{
foreach (SelectListItem sli in selectList)
{
if (sli.Value.ToLower().Trim() == selectedValue.ToLower().Trim())
{
sli.Selected = true;
break;
}
}
}
else
{
selectList = new List<SelectListItem>()
{ new SelectListItem()
{ Text = "", Value = "", Selected = true }
};
}
return helper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}
}
}
,如:
DropDownListFor