簡體   English   中英

枚舉選擇列表ID未使用ASP.NET MVC3分配給SQL

[英]Enum select list ID not being assigned to SQL using ASP.NET MVC3

我的選擇列表的基礎來自一個枚舉(在模型中):

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將它們發送到View,如以下函數所示(在控制器中):

     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(即使IT也不起作用...)

在我看來:

@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上發送到控制器的值的數據。 我不能按預期方式執行schedule.week來獲取星期的值。 我是否缺少發布從選擇列表中收集,由枚舉進行存儲並以int形式存儲的int值的步驟?

(謝謝閱讀)

安德魯

我看不到您在哪里設置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發送。 現在查看Schedule,它上面沒有稱為LocationID屬性,因此完全不獲取它的值是完全正常的。 您具有一個Location屬性,該屬性可能具有LocationID屬性。 因此,您可以像這樣修改下拉菜單:

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

現在是枚舉類型。 當我測試時,它們都為我工作。 我可以在Weekweekday屬性內的POST操作中獲取正確的整數值,這些屬性與下拉列表中選擇的值相對應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM