枚举选择列表ID没有被分配给SQL使用ASP.净MVC3

本文关键字:SQL 使用 ASP MVC3 分配 列表 选择 ID 枚举 | 更新日期: 2023-09-27 18:12:41

我的选择列表的基础来自一个枚举(在模型中):

public enum week : int
{
    Every = 0,
    First = 1,
    Second = 2,
    Third = 3,
    Fourth = 4,
    Last = 5
}
public enum weekday : int
{
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
}

我使用ViewBag将这些发送到视图,如下面的函数(在控制器中)所示:

     private void PopulateDropDown(object selectedLocation = null, )
        {
            var LocationQuery = from c in db.Location orderby c.name select c;
            ViewBag.LocationSelect = new SelectList(LocationQuery, "LocationID", "name", selectedLocation);
            var statuses = from week w in Enum.GetValues(typeof(week))
                           select new { ID = (int)w, Name= w.ToString() };
            ViewBag.week = new SelectList(statuses, "ID", "Name");
            var weekdayFormat = from weekday s in Enum.GetValues(typeof(weekday))
                           select new { ID = (int)s, Name = s.ToString() };
            ViewBag.weekday = new SelectList(weekdayFormat, "ID", "Name");
        }

对于这个例子,你可以忽略LocationQuery(即使它也不工作…)

在视图中我有:

@Html.DropDownList("locationID", (SelectList)ViewBag.LocationSelect, "Select a location")

         @Html.DropDownList("Week", (SelectList)ViewBag.TypeSelect, "Week")
         @Html.DropDownList("weekday", (SelectList)ViewBag.TypeSelect, "Day")

但是,我发送的数据没有一个被收集为模型的一部分:

public class Schedule
{
    public Schedule()
    {
        Times = new List<TimeofDay>();
    }
    public int ScheduleID { get; set; }
    public bool isDisabled { get; set; }
    [DisplayName("For Delivery")]
    public bool isDeliver { get; set; }
    public int Count { get; set; }
    public virtual ICollection<TimeofDay> Times { get; set; }
    public virtual Location Location { get; set; }
    public int Week { get; set; }
    public int weekday { get; set; }
}

渲染后,选择列表看起来像我想要的:

         <select id="Week" name="week"><option value="">Week</option>
<option value="0">Every</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
<option value="5">Last</option>
</select>

但是,我无法通过POST获得发送到控制器的值的数据。我不能通过做计划来获得一周的价值。和预期的一样。我错过了一个步骤张贴从一个选择列表收集的int值,由枚举污染,并存储为一个int?

(谢谢阅读)

安德鲁

枚举选择列表ID没有被分配给SQL使用ASP.净MVC3

我不知道你在哪里设置了ViewBag.TypeSelect.

你试过使用DropDownListFor吗?

@Html.DropDownListFor(x => x.Week, (SelectList)ViewBag.week, "Week")

编辑

另外,你的视图一定是强类型的吗?你能展示一下吗?

我假设您的POST操作将Schedule模型作为参数:

[HttpPost]
public ActionResult Foo(Schedule model)
{
    ...
}

现在让我们先看看第一个下拉菜单:

@Html.DropDownList("locationID", (SelectList)ViewBag.LocationSelect, "Select a location")

当提交时,它将在POST请求中将其值作为locationID=123发送。现在看看时间表,上面没有称为LocationID的属性,所以你没有得到它的值是完全正常的。你有一个Location性质,它大概也有一个LocationID性质。所以你可以这样修改下拉菜单:

@Html.DropDownList("Location.LocationID", (SelectList)ViewBag.LocationSelect, "Select a location")

现在是枚举类型。当我测试时,它们都对我有效。我能够在Weekweekday属性内的POST操作中获得适当的整数值,对应于下拉框中选择的值。