2012-05-12 24 views
0

我做的與數據庫優先方針ASP.net MVC 3(空類型,而不是網絡類型)...讀取數據從下拉菜單中ASP.Net MVC3

我需要的是

步驟1: 我只是使用下拉菜單來顯示公司所在的各個位置。該列表來自組織表,位置只是這個Oranization表中的一個字符串字段,

步驟2: 當用戶正在進行註冊時,下拉列表將顯示位置。現在,用戶選擇印度,然後這個值(位置名稱)應存儲在用戶登陸表...

現在如何從下拉列表中讀出的值,我希望你明白我的問題,並在此先感謝

+0

你能展示一些你已經試過的代碼嗎?如果你有一個強類型的視圖,那麼當你用'@ Html.DropDownListFor()'創建下拉列表時(如果我沒有記錯的話)它會處理將值傳遞迴發佈數據時使用的控制器。 – Jared

+0

//在我的註冊控件中,我是這樣寫的 public ActionResult Index() { ViewBag.OLocation = new SelectList(dbcontext.Organization_Details,「OName」,「OLocation」); return View(); } //並在其看來,我寫的是這樣的... //和這種觀點是強烈「用戶登陸」視圖模型 //前面的代碼部分 @ Html.Dropdownlist類型(」 OLocation「) –

+0

Hai任何評論或回覆至此爲止..... –

回答

2

我會用視圖模型:

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 
    ... 
}