强类型的RadioButtonlist

| 我想获取一些选项(例如付款方式现金,信用卡等),并将其绑定到单选按钮。我相信MVC 3中没有RadioButtonList。 另外,一旦无线电绑定,我想在编辑答案时向用户显示先前选择的选项。     
已邀请:
与往常一样,您从模型开始:
public enum PaiementMethod
{
    Cash,
    CreditCard,
}

public class MyViewModel
{
    public PaiementMethod PaiementMethod { get; set; }
}
然后是一个控制器:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}
最后是一个视图:
@model MyViewModel
@using (Html.BeginForm())
{
    <label for=\"paiement_cash\">Cash</label>
    @Html.RadioButtonFor(x => x.PaiementMethod, \"Cash\", new { id = \"paiement_cash\" })

    <label for=\"paiement_cc\">Credit card</label>
    @Html.RadioButtonFor(x => x.PaiementMethod, \"CreditCard\", new { id = \"paiement_cc\" })

    <input type=\"submit\" value=\"OK\" />
}
而且,如果您希望将其封装在帮助程序中的更通用的解决方案,则可能会找到以下答案。     
这就是我喜欢绑定RadioButtonLists的方式。视图模型包含我的强类型对象的集合。例如,也许PaymentOptions是一个代码表。与集合一起是SelectedPaymentOptionKey(如果您的主键带有Id前缀,则为Selected * Id)。最初,该键将默认为0,但在回发时,它将保留所选项目的值。
public class PaymentSelectionVM
{
  public ICollection<PaymentOption> PaymentOptions { get; set; }
  public int SelectedPaymentOptionKey { get; set; }
}

public ViewResult PaymentSelection()
{
  var paymentOptions = db.PaymentOptions.ToList();
  return View(
    new PaymentSelectionVM { 
      PaymentOptions = paymentOptions,

      //This is not required, but shows how to default the selected radiobutton
      //Perhaps you have a relationship between a Customer and PaymentOption already,
      //SelectedPaymentOptionKey = someCustomer.LastPaymentOptionUsed.PaymentOptionKey
      //  or maybe just grab the first one(note this would NullReferenceException on empty collection)
      //SelectedPaymentOptionKey = paymentOptions.FirstOrDefault().PaymentOptionKey 
    });
} 
然后在视图中:
@foreach (var opt in Model.PaymentOptions)
{          
  @*Any other HTML here that you want for displaying labels or styling*@
  @Html.RadioButtonFor(m => m.SelectedPaymentOptionKey, opt.PaymentOptionKey)
}
m.SelectedPaymentOptionKey有两个作用。首先,它将单选按钮组合在一起,以便选择是互斥的(我鼓励您使用FireBug之类的工具检查生成的html只是为了您自己的理解。关于MVC的妙处在于,生成的HTML是相当基本和标准的因此,您最终可以很容易地预测视图的行为就不难了。其次,它将保留回发中所选项目的值。 最后,在后处理程序中,我们可以使用SelectedPaymentOptionKey:
[HttpPost]
public ActionResult PaymentSelection(PaymentSelectionVM vm)
{
   currentOrder.PaymentOption = db.PaymentOptions.Find(vm.SelectedPaymentOptionKey);
   ....
}
与使用SelectListItems相比,这样做的好处是,在显示表格/表格并需要显示该对象的许多值的情况下,您可以访问该对象的更多属性。我也很喜欢Html帮助器中没有像其他方法那样传递硬编码的字符串。 缺点是您获得的单选按钮都具有相同的ID,这实际上不是一个好习惯。更改为以下即可轻松解决:
  @Html.RadioButtonFor(m => m.SelectedPaymentOptionKey, opt.PaymentOptionKey, new { id = \"PaymentOptions_\" + opt.PaymentOptionKey})
最后,对于我所见过的大多数单选按钮技术,验证有些古怪。如果我真的需要它,则每当单击单选按钮时,我都会连接一些jquery来填充一个隐藏的SelectedPaymentOptionsKey,然后在隐藏字段上放置ѭ7或其他验证。 验证问题的另一种解决方法 ASP.NET MVC 3简易验证和单选按钮 这看起来很有希望,但我没有机会对其进行测试: http://memoriesdotnet.blogspot.com/2011/11/mvc-3-radiobuttonlist-includes.html     
您应该将选项绑定到ViewModel中的SelectList,并将先前选择的选项的Selected属性设置为true     

要回复问题请先登录注册