views:

1077

answers:

1

After I upgraded to the beta, I'm having trouble with Html.RadioButtonList. Can someone show me what I'm doing wrong?

The code:

<% Html.RadioButtonList(
    "voter" + voter.Id,
    new SelectList(new[]{"yes","no","abstain"}, "yes")).Each(x => Response.Write(x)); %>

And the exception I get:

[ArgumentNullException: Value cannot be null.
Parameter name: value]
   System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value, Boolean isChecked, IDictionary`2 htmlAttributes) +214
   Microsoft.Web.Mvc.<>c__DisplayClass1.<RadioButtonListInternal>b__0(ListItem item) in c:\dd\Cicero\src\Mvc\main\src\MvcFutures\Mvc\RadioExtensions.cs:86
   System.Linq.WhereSelectListIterator`2.MoveNext() +107
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +259
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +81
   Microsoft.Web.Mvc.RadioListExtensions.RadioButtonListInternal(HtmlHelper htmlHelper, String name, SelectList selectList, Boolean usedViewData, IDictionary`2 htmlAttributes) in c:\dd\Cicero\src\Mvc\main\src\MvcFutures\Mvc\RadioExtensions.cs:88
   Microsoft.Web.Mvc.RadioListExtensions.RadioButtonList(HtmlHelper htmlHelper, String name, SelectList selectList) in c:\dd\Cicero\src\Mvc\main\src\MvcFutures\Mvc\RadioExtensions.cs:29

Many thanks in advance!
Rob

+2  A: 

Hi Rob,

It looks like you may have found a bug in the MVC framework. The other overloads of RadioButtonList seem to work just fine, but that particular overload barfs.

From looking at Reflector (and using the stack trace) I looks like things go awry at this line:

    return selectList.GetListItems().Select<ListItem, string>(delegate (ListItem item) {
    return htmlHelper.RadioButton(name, item.Value, item.Selected, htmlAttributes);
}).ToArray<string>();

In the Microsoft.Web.Mvc.RadioListExtensions.RadioButtonListInternal method. I assume this code worked fine in Preview 5?

Jason Whitehorn
Holy cow, Jason! We worked together for a short time at BT.The signature changed with the beta. Before, I used Html.RadioButtonList("voter" + voter.Id, new[] {"yes","no","abstain"}, "yes") and it worked like a champ
Rob
Yep, I remember. I actually tried every which way I could to get the method to work, but it seems that now matter what I pass in, so long as it is of type SelectList it dies. I would be interested in hearing if this works for anybody.
Jason Whitehorn