1

私の見解では、選択タグは次のようになります。

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

ストアド プロシージャを使用してデータをデータベースに渡しています。選択した値をコントローラーに渡すにはどうすればよいですか?

4

2 に答える 2

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
    ...
}
于 2013-03-25T07:44:31.563 に答える
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
 }
于 2013-03-25T10:11:03.033 に答える