2010-05-08 139 views
0

我怎樣才能得到dropdownlist的選擇值,這裏是我的代碼工作正常。在asp.net MVC獲取dropdownlist的選定值

var list = new[] { 
    new Person { Id = 1, Name = "Name1" }, 
    new Person { Id = 2, Name = "Name2" }, 
    new Person { Id = 3, Name = "Name3" } 
}; 

var selectList = new SelectList(list, "Id", "Name", 2); 
ViewData["People"] = selectList; 

<%= Html.DropDownListFor(model => model.Peoples, ViewData["People"] as IEnumerable<SelectListItem>)%> 

回答

1

model.Peoples屬性,它使用的是在DropDownListFor必須是整數類型。它將被綁定到控制器動作選擇的值到要提交這份表單:

[HttpPost] 
public ActionResult Index(YourModelType model) 
{ 
    int selectedPersonId = model.Peoples; 
    // TODO: do something with the id 
    return View(); 
} 

備註:爲Peoples屬性更加語義正確的名稱應爲PersonId