我想要获得一些选项(例如付款方式现金,信用卡等),并将其绑定到单选按钮。我相信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" /> }
如果您想要一些更通用的解决方案,将其封装在帮助器中,您可能会发现following answer有帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。