解决方法
和往常一样,你从一个模型开始:
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有帮助。