我會用視圖模型:
public class RegisterViewModel
{
public string LocationName { get; set; }
public IEnumerable<SelectListItem> Locations { get; set; }
}
然後控制器動作,這將有助於視圖:
public ActionResult Index()
{
var model = new RegisterViewModel();
model.Locations = new SelectList(dbcontext.Organization_Details, "OName", "OLocation");
return View(model);
}
那麼相應的強類型化的視圖:
@model RegisterViewModel
@using (Html.BeginForm())
{
@Html.LabelFor(x => x.LocationName)
@Html.DropDownListFor(x => x.LocationName, Model.Locations)
<button type="submit">OK</button>
}
最後當表單提交將被調用的控制器動作:
[HttpPost]
public ActionResult Index(RegisterViewModel model)
{
// model.LocationName will contain the selected location here
...
}
你能展示一些你已經試過的代碼嗎?如果你有一個強類型的視圖,那麼當你用'@ Html.DropDownListFor()'創建下拉列表時(如果我沒有記錯的話)它會處理將值傳遞迴發佈數據時使用的控制器。 – Jared
//在我的註冊控件中,我是這樣寫的 public ActionResult Index() { ViewBag.OLocation = new SelectList(dbcontext.Organization_Details,「OName」,「OLocation」); return View(); } //並在其看來,我寫的是這樣的... //和這種觀點是強烈「用戶登陸」視圖模型 //前面的代碼部分 @ Html.Dropdownlist類型(」 OLocation「) –
Hai任何評論或回覆至此爲止..... –