2011-04-14 90 views
19

我想获得一些选择(比如支付方式现金,信用卡等)并将它们绑定到单选按钮。我相信MVC 3中没有RadioButtonList。强类型RadioButtonlist

另外,一旦收音机被绑定,我想在编辑答案时向用户显示先前选择的选项。

+0

有我的博客上的HTML帮助,可以帮助HTTP:/ /jonlanceley.blogspot.com/2011/06/mvc3-radiobuttonlist-helper.html – Jon 2011-07-03 08:46:30

回答

37

与往常一样,你从这个模型开始:

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有帮助。

+3

非常感谢Darin.But我的要求是绑定数据库值的单选按钮并将Text/Value对分配给单选按钮,以便可以分配它模型的属性。 – Vivek 2011-04-14 12:01:42

+0

非常好的工作! – Funky 2012-05-18 14:57:58

+0

完美!一直在寻找这一段时间 – bcahill 2012-06-20 17:54:34

0

你应该绑定你的选项中的SelectList视图模型将Selected设置属性为true,以前选择的选项

4

这是我怎么样绑定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,并将[Required]或其他验证放置在隐藏字段上。

另一个解决方法的验证问题 ASP.NET MVC 3 unobtrusive validation and radio buttons

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