how to select a radio button by default - asp.net mvc strongly typed html helpers
Asked Answered
H

3

10

I have a radio button list like this:

<%=Html.RadioButtonFor(m => m.Gender,"Male")%>

I want this button selected by default. How do I do this?

Hanes answered 30/6, 2010 at 12:52 Comment(0)
T
20
<%: Html.RadioButtonFor(x => x.Gender, "Male", new { @checked = "checked" }) %>

or in the controller action that renders this view:

model.Gender = "Male";
return View(model);
Tc answered 30/6, 2010 at 13:2 Comment(2)
Sorry Darin, This is not working. Is there any alternate solution ?Hanes
Sure it is working, I've just created a sample ASP.MVC project, put a view model with a single property Gender (of type string), and in the view used HTML attributes to set the checked property.Tc
H
6

if u are using Razor view strong you can use use radio button like this

  @Html.RadioButtonFor(model => model.PrintOrder, "Sequential", new {@checked="true"})    Sequential  

as your need correct it as Razor view

  @Html.RadioButtonFor(x => x.Gender, "Male", new { @checked = "checked" })

Aspx view

  <%: Html.RadioButtonFor(x => x.Gender, "Male", new { @checked = "checked" }) %>
Heidelberg answered 2/8, 2013 at 6:32 Comment(1)
not working dudeDereism
W
3

TRY:

<%: Html.RadioButtonFor(x => x.Gender, "Male", new { Checked = true }) %>

Worked in my case.

Wheels answered 4/6, 2018 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.