bguler
bguler

Reputation: 247

Get dynamic radio buttons values

I'm doing survey system on asp.net mvc 4. How can i get all radio buttons data in Controller ?

Example

$(document).ready(function(){
   var AnswerCounter = 0;
   $("#answer-add").click(function(){

    var answertext = $("#answer-text").val();
    var answerContent = $('<p>', {
        class:'external',
    });

    var inputAnswer = $('<input>',{
        id:'answer-'+AnswerCounter,
        name:'rb',
        class:'answer-radio'
    }).attr("type","radio").appendTo(answerContent);

    var labelAnswer = $('<label>').attr('for','answer-'+AnswerCounter).text(answertext).appendTo(answerContent);                

    $('#answers').append(answerContent);
    AnswerCounter++;
});

});

You can see in the example buttons created by jquery. so i can use model binding.

Thanks.

Upvotes: 0

Views: 1787

Answers (2)

monkeyhouse
monkeyhouse

Reputation: 2896

The HTML typically geneated for MVC3 radiobuttons look like this

<input name="FeedingTime" id="FeedingTime" type="radio" value="Morning"/>
<input name="FeedingTime" id="FeedingTime" type="radio" value="Afternoon"  checked="checked"/>

and when the radio button group posts, it will bind to the variable that matches the name.

[HttpPost]
public ActionResult Index (string FeedingTime){
  //feeding time here is "Afternoon"
}

so to make your code bind correctly,

  • set the value of the input 'value' attribute in your script, and

  • have a variable in the ActionResult that matches the 'name' attribute in the html input element

Upvotes: 2

monkeyhouse
monkeyhouse

Reputation: 2896

EDIT: This answer is no longer relevant to the question being asked.

to get the value from radio buttons you can bind to them like you would a string variable; suppose in your view you have

@Html.LabelFor(m => m.FeedingTime,"Morning")
@Html.RadioButtonFor(m => m.FeedingTime, "Morning")<br />
@Html.LabelFor(m => m.FeedingTime,"Afternoon")
@Html.RadioButtonFor(m => m.FeedingTime, "Afternoon")<br />
@Html.LabelFor(m => m.FeedingTime,"Night")
@Html.RadioButtonFor(m => m.FeedingTime, "Night")

when you post back, you would capture this in a string variable public ActionResult(..., string FeedingTime, ...){

 }

normally this variable is embedded in a viewmodel such as

public class AnimalViewModel
{
    public string Name { get; set; }
    public string Type { get; set; }
    public string FavoriteColor { get; set; }
    public string FeedingTime { get; set; }
}

so that when you post

[HttpPost]
public ActionResult Index(AnimalViewModel viewModel){
  ... 
}

it binds the viewmodel to the data automagically.

To answer your question more directly. I dont think theres a way to detect all radiobutton inputs on a page, you can only detect the results from that are posted from the radio button. There is no indication that the string posted was once the answer to a radiobutton. So, if you want to know the values of all radiobuttons on the page, you will simply have to check the values of the strings which are posted.

Upvotes: 1

Related Questions