2013-06-21 48 views
2

如何在按鈕被點擊時調用控制器動作併發送在下拉列表中選擇哪些值?這裏是我的.cshtml的樣子。這只是一個例子,通常我需要在單擊按鈕時及時從當前視圖中收集大量數據。如何在點擊按鈕時將數據從視圖發送到控制器

<body> 
    <div> 
     @Html.DropDownList("Name") 
     <br /> 
     @Html.DropDownList("Age") 
     <br /> 
     @Html.DropDownList("Gender") 
     <br /> 
     @using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post)) 
     { 
      <input type="submit" value="Find" /> 
     } 
    </div> 
</body> 
+2

這似乎是一個非常基本的問題之外你的輸入 - 你試圖尋找一個教程? –

回答

2

爲了使數據提交給所述控制器,所述輸入必須的<form>標記內出現。

例如:

<body> 
    <div> 
     @using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post)) 
     { 

      @Html.DropDownList("Name") 
      <br /> 
      @Html.DropDownList("Age") 
      <br /> 
      @Html.DropDownList("Gender") 
      <br /> 
      <input type="submit" value="Find" /> 
     } 
    </div> 
</body> 
+0

與我的答案几乎相同。必須upvote! –

+0

Thx。爲了幫助別人如何在控制器中使用它:public ActionResult FindPerson(FormCollection form) – watbywbarif

2

的@using(Html.BeginForm( 「是FindPerson」, 「myController的」,FormMethod.Post)),你應該把你的輸入中。

你有表格

@using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post)) 
    { 
    @Html.DropDownList("Name") 
    <br /> 
    @Html.DropDownList("Age") 
    <br /> 
    @Html.DropDownList("Gender") 
    <br /> 

     <input type="submit" value="Find" /> 
} 
1

之外你首先輸入u需要的模型綁定您的數據。

public class TestModel 
    { 
     public string Age { get; set; } 
     public string Gender { get; set; } 
     ... 
    } 

那麼你需要用你的dropLists在表單標籤

<form method='post'> 
@Html.DropDownList("Age") 
</form> 

和行動recive發佈的數據

[HttpPost] 
     public ActionResult YourAction(TestModel model)//selected data here 
     { 

     } 
0

的@using內(Html.BeginForm( 「NameOfActionMethod」 「ControllerName」,FormMethod.Post))你應該把你的輸入。

你有表格

@using (Html.BeginForm("NameOfActionMethod", "ControllerName", FormMethod.Post)) 
{ 
    <input type="submit" value="Find" /> 
} 
相關問題