2

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?

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
tripathy
  • 375
  • 2
  • 6
  • 23

1 Answers1

8

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:")
granit
  • 570
  • 4
  • 9
  • can you please see this one http://stackoverflow.com/questions/43518802/dropdownlistfor-to-be-auto-selected-on-the-basis-of-a-value-of-string-asp-net-mv too – tripathy Apr 20 '17 at 11:55