强类型RadioButtonlist

Viv*_*vek 19 radiobuttonlist razor asp.net-mvc-3

我想获得一些选项(比如付款方式现金,信用卡等)并将这些选项绑定到单选按钮.我相信MVC 3中没有RadioButtonList.

此外,一旦绑定了无线电,我想在编辑答案时向用户显示先前选择的选项.

Dar*_*rov 37

一如既往,您从一个模型开始:

public enum PaiementMethod
{
    Cash,
    CreditCard,
}

public class MyViewModel
{
    public PaiementMethod PaiementMethod { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后一个控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后一个观点:

@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" />
}
Run Code Online (Sandbox Code Playgroud)

如果您想要一些更通用的解决方案,将其封装在帮助器中,您可能会发现以下答案很有帮助.

  • 好吧,谢谢Darin.但我的要求是从数据库值绑定单选按钮,并将Text/Value对分配给单选按钮,以便将其分配给Model的属性. (3认同)

Aar*_*nLS 5

这就是我喜欢绑定 RadioButtonList 的方式。视图模型有我的强类型对象的集合。例如,PaymentOptions 可能是一个代码表。与该集合一起的是 SelectedPaymentOptionKey(或 Selected*Id,如果您在主键上添加 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 
    });
} 
Run Code Online (Sandbox Code Playgroud)

然后在视图中:

@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)
}
Run Code Online (Sandbox Code Playgroud)

m.SelectedPaymentOptionKey 有两个用途。首先,它将单选按钮分组在一起,以便选择是互斥的(我鼓励您使用像 FireBug 这样的东西来检查生成的 html,只是为了您自己的理解。MVC 的美妙之处在于生成的 HTML 是相当基本和标准的因此,您最终能够预测您的视图的行为应该不难。这里没有什么神奇之处。)。其次,它将在回发时保存所选项目的值。

最后,在后处理程序中,我们有可用的 SelectedPaymentOptionKey:

[HttpPost]
public ActionResult PaymentSelection(PaymentSelectionVM vm)
{
   currentOrder.PaymentOption = db.PaymentOptions.Find(vm.SelectedPaymentOptionKey);
   ....
}
Run Code Online (Sandbox Code Playgroud)

与使用 SelectListItems 相比,这样做的优点是,在显示网格/表格并需要显示对象的许多值的情况下,您可以访问对象的更多属性。我还喜欢 Html 助手中没有像其他方法那样传递硬编码字符串。

缺点是您得到的单选按钮都具有相同的 ID,这并不是一个好的做法。通过更改为以下内容可以轻松解决此问题:

  @Html.RadioButtonFor(m => m.SelectedPaymentOptionKey, opt.PaymentOptionKey, new { id = "PaymentOptions_" + opt.PaymentOptionKey})
Run Code Online (Sandbox Code Playgroud)

最后,对于我见过的大多数单选按钮技术来说,验证都有点奇怪。如果我真的需要它,每当单击单选按钮时,我都会连接一些 jquery 来填充隐藏的 SelectedPaymentOptionsKey,并将验证[Required]或其他验证放在隐藏字段上。

验证问题的另一种解决方法 ASP.NET MVC 3 不显眼的验证和单选按钮

这看起来很有希望,但我还没有机会测试它: http ://memoriesdotnet.blogspot.com/2011/11/mvc-3-radiobuttonlist-include.html