Search code examples
asp.net-mvc-2radiobuttonlist

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?


Solution

  • 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.