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 
}