software
software

Reputation: 728

default check in radiobutton list in MVC?

i am passing list of values in radiobutton ( 5 values). but i want one among them should be selected/checked by default. How can i do that?

Upvotes: 1

Views: 2811

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038920

You could set the view model property to the required value. Example:

public class MyViewModel
{
    public string Value { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel { Value = "No" };
        return View(model);
    }
}

and in the view:

<%= Html.RadioButtonFor(x => x.Value, "Yes", new { id = "yes" }) %> Yes
<%= Html.RadioButtonFor(x => x.Value, "No", new { id = "no" }) %> No
<%= Html.RadioButtonFor(x => x.Value, "Maybe", new { id = "maybe" }) %> Maybe

which will select the No button.

Upvotes: 5

Related Questions