我的循环选择列表中出现 MVC 3 控制器语法错误

本文关键字:MVC 控制器 语法 错误 循环 选择 列表 我的 | 更新日期: 2023-09-27 18:31:29

我在语法上遇到了一些问题,在我的控制器内为一个月中的几天创建一个选择列表。我让月份和年份工作,但 for 循环给我带来了一些麻烦。

Visual Studio 给我的错误是:"无效的初始值设定项成员声明符"和"名称 Days 在当前上下文中不存在"。

视图模型

public class DateSearchViewModel
{
    public int SelectedMonth { get; set; }
    public IEnumerable<SelectListItem> Months { get; set; }
    public int SelectedDay { get; set; }
    public IEnumerable<SelectListItem> Days { get; set; }
    public int SelectedYear { get; set; }
    public IEnumerable<SelectListItem> Years { get; set; }
}

控制器

    [ChildActionOnly]
    public ActionResult _Header()
    {
        var DateVM = new DateSearchViewModel
        {
            //Set selected value of month.
            SelectedMonth = 5,
            Months = new List<SelectListItem> 
            {
                new SelectListItem {Text = "Jan", Value = "1"},
                new SelectListItem {Text = "Feb", Value = "2"},
                new SelectListItem {Text = "Mar", Value = "3"},
                new SelectListItem {Text = "Apr", Value = "4"},
                new SelectListItem {Text = "May", Value = "5"},
                new SelectListItem {Text = "Jun", Value = "6"},
                new SelectListItem {Text = "Jul", Value = "7"},
                new SelectListItem {Text = "Aug", Value = "8"},
                new SelectListItem {Text = "Sep", Value = "9"},
                new SelectListItem {Text = "Oct", Value = "10"},
                new SelectListItem {Text = "Nov", Value = "11"},
                new SelectListItem {Text = "Dec", Value = "12"},
            },
            //Set selected value of Day.
            SelectedDay = 13,
            Days = new List<SelectListItem>
            {
                for (var i = 1; i <= 31; i++)
                {
                    Days.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString()});
                }
            },
            //Set selected value of Year.
            SelectedYear = 2010,
            Years = new List<SelectListItem> 
            {
                new SelectListItem {Text = "2009", Value = "2009"},
                new SelectListItem {Text = "2010", Value = "2010"},
                new SelectListItem {Text = "2011", Value = "2011"},
                new SelectListItem {Text = "2012", Value = "2012"},
            }
        };
        return PartialView(DateVM);
    }

视图模型:

@Html.DropDownListFor(x => x.SelectedMonth, Model.Months) / @Html.DropDownListFor(x => x.SelectedDay, Model.Days) / @Html.DropDownListFor(x => x.SelectedYear, Model.Years)

我的循环选择列表中出现 MVC 3 控制器语法错误

正如编译器试图告诉你的那样,你不能那样做。
初始值设定项只能包含表达式。

相反,您可以使用 LINQ:

Days = Enumerable.Range(1, 31)
                 .Select(i => new SelectListItem { Text = i.ToString() }))

不能在初始值设定项中放置循环:

Days = new List<SelectListItem>
{
    for (var i = 1; i <= 31; i++)
    {
        Days.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString()});
    }
},

因此,只需实例化它并在以后使用循环:

var DateVM = new DateSearchViewModel 
{
    ...
    Days = new List<SelectListItem>(),
    ...
}
for (var i = 1; i <= 31; i++)
{
    DateVM.Days.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString()});
}