StackTrace
StackTrace

Reputation: 9416

Get value of selected RadioButton in asp.net mvc

<td>
  @if (ViewBag.MyList as System.Collections.IEnumerable != null)
  {
    int i = 1;
    foreach (var m in new  SelectList(ViewBag.MyList as System.Collections.IEnumerable, "Value", "Text", 0))
    {
      @Html.RadioButton("MyList" + i, m.value)
      @Html.Label(m.Text)
      i += 1;
    }
  }
</td>

If i have code similar to the above in an ASP.NET MVC view, how do I get the value of the selected RadioButton in jQuery? Some thing along the line $.trim($("#MyList").val()) doesn't seem right as #MyList (i.e name of the drop-down will be dynamic depending on the value of variable i)

Upvotes: 0

Views: 1386

Answers (2)

malkam
malkam

Reputation: 2355

Try this:

$("input[id^='MyList']:checked").val()

Upvotes: 0

Andrei
Andrei

Reputation: 56688

jQuery has "starts with" selector for such case. To get the selected item, use "checked":

var v = $.trim($("[id^='MyList']:checked").val());

Upvotes: 1

Related Questions