2

How can I invoke controller action and send which values are selected in drop down lists in time when button was clicked? Here is example how my .cshtml looks like. This is just example, generally I need to collect much data from current view in time when button was clicked.

<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>
1
  • 2
    This seems like a very basic question - have you tried looking for a tutorial? Commented Jun 21, 2013 at 13:36

4 Answers 4

2

In order for the data to be submitted to the controller, the inputs must appear within the <form> tag.

For example:

<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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thx. To help someone else how to use this in controller: public ActionResult FindPerson(FormCollection form)
2

inside the @using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post)) you should put your inputs.

You have your inputs outside the Form

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

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

Comments

1

First u need the Model to bind your data.

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

then you need to wrap your dropLists in form tag

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

and action to recive posted data

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

        }

Comments

0

inside the @using (Html.BeginForm("NameOfActionMethod", "ControllerName", FormMethod.Post)) you should put your inputs.

You have your inputs outside the Form

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.