使用storedprocedure将选择标记选择的值插入数据库

时间:2013-03-25 07:38:40

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3-areas asp.net-mvc-2-validation

在我看来,我的选择标签是这样的:

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

我正在使用存储过程将数据传递到数据库。如何将所选值传递给控制器​​?

2 个答案:

答案 0 :(得分: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>
}

最后你可以有一个相应的控制器动作,表单将被提交到该控制器动作并将视图模型作为参数:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // model.Value will contain the selected value here
    ...
}

答案 1 :(得分: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
 }