我做了一個簡單的mvc 3網站。它工作正常,但數據庫和類是凌亂的,因爲我需要做下拉列表,我不知道如何實現使用不同風格的下沉列表。Mvc代碼第一款
這裏的東西怎麼看起來像:
public class Departments
{
public int ID {get; set;}
...
}
public class Users
{
public int ID {get; set;}
...
public Department Dept {get; set;}
public List<Department> DeptList {get; set;}
public string SelectedDept {get; set;}
}
然後我用DropDownListFor幫手。可悲的是,我對這個代碼的第一件事是新的。所以,現在我在數據庫中得到了這兩個額外的不必要的列。任何其他方式來做這個更清潔/更好?
感謝您的提示。對於字符串SelectedDept,它是@ Html.DropDownListFor(model => model.SelectedDept,new SelectList(Model.DeptList,「Value」,「Text」))。任何樣本的幫手下拉列表? – Bahamut
使用類似於@ Html.DropDownListFor(x => x.DeptId,new SelectList(ViewBag.DeptList,「DerparmentName」,「id」),「Choose:」) – Daveo