我想要一些选择(比如支付方式,现金,信用卡等)并将这些绑定到单选按钮。我相信在MVC3中没有RadioButtonList。
此外,一旦无线电被绑定,我希望在编辑答案时向用户显示之前选择的选项。
发布于 2011-04-14 16:22:20
与往常一样,您可以从模型开始:
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" />
}如果您想要一些更通用的解决方案,将其封装在帮助器中,您可能会发现following answer很有帮助。
发布于 2012-07-11 19:38:58
这就是我喜欢绑定RadioButtonLists的方式。视图模型有一个我的强类型对象的集合。例如,PaymentOptions可能是一个代码表。与该集合一起的是一个SelectedPaymentOptionKey (如果在主键前面加上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
});
} 然后在视图中:
@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是相当基础和标准的,因此您最终应该可以轻松预测视图的行为。这里几乎没有什么魔力。)其次,它将在回发时保存选定项的值。
最后,在post处理程序中,我们有了可用的SelectedPaymentOptionKey:
[HttpPost]
public ActionResult PaymentSelection(PaymentSelectionVM vm)
{
currentOrder.PaymentOption = db.PaymentOptions.Find(vm.SelectedPaymentOptionKey);
....
}与使用SelectListItems相比,这样做的优势在于,在显示网格/表格并需要显示对象的许多值的情况下,您可以访问更多的对象属性。我还喜欢在Html helper中传递硬编码字符串,这一点与其他方法不同。
缺点是您得到的单选按钮都具有相同的ID,这并不是一个真正的好做法。这很容易解决,只需将其更改为:
@Html.RadioButtonFor(m => m.SelectedPaymentOptionKey, opt.PaymentOptionKey, new { id = "PaymentOptions_" + opt.PaymentOptionKey})最后,对于我见过的大多数单选按钮技术,验证都有点奇怪。如果我真的需要它,我会连接一些jquery,以便在单选按钮被单击时填充隐藏的SelectedPaymentOptionsKey,并将[Required]或其他验证放在隐藏的字段上。
验证问题ASP.NET MVC 3 unobtrusive validation and radio buttons的另一种解决方法
这看起来很有希望,但我还没有机会对其进行测试:http://memoriesdotnet.blogspot.com/2011/11/mvc-3-radiobuttonlist-including.html
发布于 2011-05-04 20:00:40
您应该将选项绑定到ViewModel中的SelectList,并将以前选择的选项的选定属性设置为true
https://stackoverflow.com/questions/5660377
复制相似问题