2013-07-15 85 views
4

我正在生成单选按钮列表,然后尝试在加载时选择一个选项,如下所示。RadioButtonFor中的默认选择

Foreach循环在View

@foreach (var myListItem in Model.MyList) 
{ 
    @Html.RadioButtonFor(m => m.MyType,myListItem.MyType, new {id = myListItem.MyType, @Checked = (Model.MyTypeId == myListItem.MyTypeId) }) 
    @myListItem.MyType 
} 

Eventhough的HTML正确生成(见下文)。即使在Model.MyTypeId = 0时,第二个选项也会被选中而不是第一个。

生成的HTML视图为

<input id="0" name="MyType" value="Option One" CHECKED="True" type="radio">Option One 
<input id="1" name="MyType" value="Option Two " CHECKED="False" type="radio">Option Two  

请建议我怎么回事,可以选择通过deafult所需的单选按钮选项。

回答

5

实际上HTML不正确。你需要做的东西沿着这些线路更多:

@foreach (var myListItem in Model.MyList) 
{ 
    if (Model.MyTypeId == myListItem.MyTypeId) 
    { 
     @Html.RadioButtonFor(m => m.MyType,myListItem.MyType, 
      new 
      { 
       id = myListItem.MyType, 
       @Checked = "" 
      }) 
    } 
    else 
    { 
     @Html.RadioButtonFor(m => m.MyType,myListItem.MyType, 
      new 
      { 
       id = myListItem.MyType, 
      }) 
    } 
    @myListItem.MyType 
} 

虽然我无法验证确切输出,它应该是这个样子:

<input id="0" name="MyType" value="Option One" CHECKED type="radio"> 

您可能需要使用null来得到它产生CHECKED没有="",但那也没关系。看,这不是这是确认,它是属性本身,所以这就是为什么第二个被检查。

+0

如果没有if-else就可以更好。 – San

+0

很确定,如果你有正确的“For”的第一部分,当你从列表中加载页面时,它将选择那个。 – mmcrae

2

任何时候我需要一个从查询创建的单选按钮列表,我总是达到这个RadioButtonListFor扩展方法。工程就像一个魅力:

// jonlanceley.blogspot.com/2011/06/mvc3-radiobuttonlist-helper.html 
public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> listOfValues) 
{ 
    var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 
    var sb = new StringBuilder(); 
    sb.Append("<span class='RadioButtonListFor'> "); 

    if (listOfValues != null) 
    { 
     // Create a radio button for each item in the list 
     foreach (SelectListItem item in listOfValues) 
     { 
      // Generate an id to be given to the radio button field 
      var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value); 

      // Create and populate a radio button using the existing html helpers 

      var htmlAttributes = new Dictionary<string, object>(); 
      htmlAttributes.Add("id", id); 

      if (item.Selected) 
       htmlAttributes.Add("checked", "checked"); 

      var radio = htmlHelper.RadioButtonFor(expression, item.Value, htmlAttributes); 


      // Create the html string that will be returned to the client 
      // e.g. <label<input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" />Line1</label> 
      sb.AppendFormat("<label>{0} {1}</label> ", radio, HttpUtility.HtmlEncode(item.Text)); 
     } 
    } 

    sb.Append(" </span>"); 
    return MvcHtmlString.Create(sb.ToString()); 
} 

现在,你可以从你的记忆有任何收集产生的单选按钮,通常像这样在您的视图模型的属性:

public int SelectedPaymentMethodId { get; set; } 

public IEnumerable<SelectListItem> PaymentMethodChoices 
{ 
    get 
    { 
      return from x in dataSourceFoo 
       select new SelectListItem { 
         Text = x.TextThing, Value = x.Id, Selected = (x.Id == SelectedPaymentMethodId) 
        }; 
    } 
} 

而且你的视图被简单如:

@Html.RadioButtonListFor(model => model.SelectedPaymentMethodId, Model.PaymentMethodChoices)