【问题标题】:Html.RadioButtonFor MVC BindingHtml.RadioButtonFor MVC 绑定
【发布时间】:2017-09-14 07:36:01
【问题描述】:

我的视图有一个字符串属性。通过操作结果执行获取请求并发送到视图 - 它看起来像这样:

public ActionResult TeaCoffeView(string teaOrCoffee)
{
    MyModel model = new MyModel();
    //do some stuff
    //teaOrCoffee has two values , if "T" i have
    //to make the view as tea radio button selected 
    //or if it has "C" then coffee radio button selected 

    return View(model);
}

我的看法.cshtml

@Html.Label("tea:")
@Html.RadioButtonFor(m => m._teaOrCoffee, Model._teaOrCoffee)  
@Html.Label("coffe:")
@Html.RadioButtonFor(m => m._teaOrCoffee, Model._teaOrCoffee) 

型号

public string _teaOrCoffee {get; set;}

如何将值与@Html.RadioButtonFor 绑定,以便在加载时显示为选中状态?

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4 razor radio-button


【解决方案1】:

使用@Html.RadioButtonFor() 中的第二个参数设置应选择的值。

@Html.RadioButtonFor(model => model._teaOrCoffee, "T") @Html.Label("tea:")
@Html.RadioButtonFor(model => model._teaOrCoffee, "C") @Html.Label("coffe:")

【讨论】:

猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多