我是新來的MVC,如此忍受我交手......MVC3 - 掌握DropDownList的
我有我的新形式\視圖中工作(創建並添加客戶端) 但現在我希望讓用戶如此指定新客戶端所在的國家/地區從下拉列表中刪除。但我很確定我該如何做到這一點?
視圖模型
public class ClientNew
{
public string Company { get; set; }
public string Address { get; set; }
//New
public IEnumerable<CountryList> Country{ get; set; }
}
public class CountryList
{
public string Id { get; set; }
public string Name { get; set; }
}
控制器 (這是爲可能是錯誤的,這是做的最好的方法是什麼?)
public ActionResult New()
{
var cl= new List<CountryList>();
cl.Add(new CountryList(){Id = "abcd",Name = "UK"});
cl.Add(new CountryList() { Id = "abce", Name = "USA" });
var model = new ViewModels.ClientNew();
model.Country= cl;
return View("New", model);
}
查看(不知道如何在下探此)
Html.DropDownList("Id" ??????)
作爲替代方案,你可以有你的視圖模型的IEnumerable國家,然後就做一個LINQ在Action中進行投影來設置它。國際海事組織這是一個更好的方法,因爲它是可重構的,不依賴於魔術字符串。 –