简体   繁体   English

Html.RadioButtonFor MVC绑定

[英]Html.RadioButtonFor MVC Binding

My view has one string property. 我的视图有一个字符串属性。 Doing a get request through an action result and sending to view -- it looks like this: 通过操作结果执行get请求并发送到视图 - 它看起来像这样:

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);
}

my view .cshtml 我的观点.cshtml

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

Model 模型

public string _teaOrCoffee {get; set;}

How to bind the value with @Html.RadioButtonFor so that when loads it should show as selected? 如何将值与@Html.RadioButtonFor绑定,以便在加载时它应显示为选中状态?

Use the second argument in @Html.RadioButtonFor() to set the value for which it should be selected. 使用@Html.RadioButtonFor()的第二个参数来设置应该选择它的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM