简体   繁体   中英

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:

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

@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?

Use the second argument in @Html.RadioButtonFor() to set the value for which it should be selected.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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