1

數據庫使用我認爲我的選擇標記是這樣的:插入選擇標記選擇的值爲StoredProcedure

<select name="selectedItem" id="selecttag" onchange="GetSelectedItem()"> 
    <option value="select">Select any value</option> 
    <option value="Objective">Objective</option> 
    <option value="Subjective">Subjective</option> 
</select> 

我使用的存儲過程將數據傳遞到數據庫。我怎樣才能將我選擇的值傳遞給我的控制器?

回答

0

你可以使用一個視圖模型:

public class MyViewModel 
{ 
    public string Value { get; set; } 
    public IEnumerable<SelectListItem> { get; set; } 
} 

,那麼你可以有一個控制器,它被填充這個模型,並把它傳遞給視圖:

public ActionResult Index() 
{ 
    var model = new MyViewModel(); 
    // TODO: you could load the values from your database here 
    model.Values = new[] 
    { 
     new SelectListItem { Value = "Objective", Text = "Objective" }, 
     new SelectListItem { Value = "Subjective", Text = "Subjective" }, 
    }; 
    return View(model); 
} 

,然後有一個相應的強類型您可以在其中使用Html.DropDownListFor幫手:

@model MyViewModel 

@using (Html.BeginForm()) 
{ 
    @Html.DropDownListFor(x => x.Value, Model.Values, "Select any value"); 
    <button type="submit">OK</button> 
} 

最後你會找到d具有到表單將提交一個相應的控制器操作和取視圖模型作爲參數:

[HttpPost] 
public ActionResult Index(MyViewModel model) 
{ 
    // model.Value will contain the selected value here 
    ... 
} 
0
[HttpPost] 
public ActionResult Index(MyViewModel model,string selectedItem) //"selectedItem" is the name of your drop down list. 
{ 
    //here by "selectedItem" variable you can get the selected value of dropdownlist 
}