0
在整個我的應用程序中使用了這些,但碰到這個問題。綁定下拉列表值
我有這兩種模式:
public class vwLkUpDepartmentCategory
{
[Key]
public int DepartmentCategoryID { get; set; }
public string DepartmentCategory { get; set; }
public Byte IsActive { get; set; }
}
public class vwDimDepartments
{
[Key]
public Int64 DepartmentID { get; set; }
public string DepartmentName { get; set; }
public int DepartmentCategoryID { get; set; }
public string DepartmentCategory{ get; set; }
public string Departments { get; set; }
public Byte IsActive { get; set; }
}
控制器
我設置的下拉列表的值在viewbag
ViewBag.DepartmentDropDown = new SelectList(db.vwLkUpDepartmentCategory.Where(x => x.IsActive == 1).ToList(), "DepartmentCategoryID", "DepartmentCategory");
在視圖中我有
<div>@Html.LabelFor(model => model.DepartmentCategoryID)</div>
<div>@Html.DropDownListFor(model => model.DepartmentCategoryID, new SelectList(ViewBag.DepartmentDropDown, "DepartmentCategoryID", "DepartmentCategory"), "", new { @class = "dropdown", id = "txtDepartmentCategoryID" })</div>
<div>@Html.ValidationMessageFor(model => model.DepartmentCategoryID, "", new { @class = "text-danger" })</div>
但當我運行它時,我得到一個錯誤,說
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'DepartmentCategoryID'.
這是因爲我有departmentId作爲視圖中的關鍵值?
如果我沒有在視圖中明確定義departmentCategoryID字段,那麼它需要第一個部門並始終使用該部門。
這是最好的方法是什麼?
只好在選擇列表的前面添加新的視圖中添加)之後ViewBag.DepartmentCategoryID運行代碼,但它沒有解決它。 IT現在返回一個下拉與System.Web.Mvc.SelectListItem – JQuery
使用'
謝謝,雖然這在下拉菜單中顯示正確的類別,它不捕獲正確的ID。它正在獲取CategoryID的第一個值,並因某種原因使用它。 – JQuery