gpt4 book ai didi

c# - 设置 Html.DropDownList() 的默认值

转载 作者:太空狗 更新时间:2023-10-29 22:27:37 26 4
gpt4 key购买 nike

当它回发时,我收到以下错误:

The ViewData item that has the key 'ClosingDateDay' is of type 'System.Int32' but must be of type 'IEnumerable'. Any ideas?

这是我的 Controller :

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)

最佳答案

在构造您的 SelectListItem 类时,将您最初想要选择的项目的 Selected 属性设置为 true。

关于c# - 设置 Html.DropDownList() 的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11145517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com