Reputation: 728
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
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