加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 强类型的RadioButtonlist

发布时间:2020-12-16 00:46:45 所属栏目:asp.Net 来源:网络整理
导读:我想要获得一些选项(例如付款方式现金,信用卡等),并将其绑定到单选按钮。我相信MVC 3中没有RadioButtonList。 另外,一旦收音机被绑定,我想在编辑答案时向用户显示先前选择的选项。 解决方法 和往常一样,你从一个模型开始: public enum PaiementMethod{
我想要获得一些选项(例如付款方式现金,信用卡等),并将其绑定到单选按钮。我相信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有帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读