Willy
Willy

Reputation: 10638

ASP.NET MVC 4 Passing data to the controller

I have a form, with a textbox and some radio buttons (Only 1 radio button can be selected at once) so I want to submit to the controller, the person name and the radio button select, that is value 1, 2 or 3 depending on the selection (1:Programmer, 2:Project Manager, 3:Analyst) but I do not know how to pass only the value of the radio button selected instead of the value of all them. how to get rid of this?

for example, it person name is David and the selected radio button is Programmer, I want to pass (David,1), if selected radio button is Project Manager (David, 2) and if selected radio button is Analyst (David, 3) to the controller.

   @using (Html.BeginForm("AddPerson", "AddToDatabase", FormMethod.Get))
   {
       <div id="Radio">
           @Html.TextBox("PersonName")
           @Html.RadioButton("Programmer")
           @Html.RadioButton("Project Manager")
           @Html.RadioButton("Analyst")
       </div>

       <input type="submit" value="@Resource.ButtonTitleAddPerson" />
   }

The controller signature is:

public bool AddPerson(string name, int charge)

Upvotes: 0

Views: 2150

Answers (1)

Andrey Gubal
Andrey Gubal

Reputation: 3479

Try to use @Html.RadioButton in other way:

@Html.RadioButton("Profession", "1") Programmer
@Html.RadioButton("Profession", "2") Project Manager
@Html.RadioButton("Profession", "3") Analyst

In your approach you receive three unconnected radio buttons with different id. And form thinks that they are different inputs and sends them all.

Upvotes: 1

Related Questions