Skip to main content
deleted 2 characters in body
Source Link
NoWar
  • 37.8k
  • 85
  • 341
  • 520

Here is a more complete example using a fieldset for accessibility reasons and specifying the first button as the default. Without a fieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public stringbool IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked argument to the anonymous object to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing true and false with the string values.

Here is a more complete example using a fieldset for accessibility reasons and specifying the first button as the default. Without a fieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public string IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked argument to the anonymous object to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing true and false with the string values.

Here is a more complete example using a fieldset for accessibility reasons and specifying the first button as the default. Without a fieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public bool IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked argument to the anonymous object to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing true and false with the string values.

added 11 characters in body
Source Link
Daniel Imms
  • 50.6k
  • 19
  • 157
  • 170

Here is a more complete example using a fieldsetfieldset for accessibility reasons and specifying the first button as the default. Without a fieldsetfieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public string IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked parameters@checked argument to the anonymous object like so to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing truetrue and falsefalse with the string values.

Here is a more complete example using a fieldset for accessibility and specifying the first button as the default. Without a fieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public string IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked parameters the anonymous object like so to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing true and false with the string values.

Here is a more complete example using a fieldset for accessibility reasons and specifying the first button as the default. Without a fieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public string IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked argument to the anonymous object to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing true and false with the string values.

Source Link
Daniel Imms
  • 50.6k
  • 19
  • 157
  • 170

Here is a more complete example using a fieldset for accessibility and specifying the first button as the default. Without a fieldset, what the radio buttons are for as a whole can not be programmatically determined.

Model

public class MyModel { public string IsMarried { get; set; } } 

View

<fieldset> <legend>Married</legend> @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" }) @Html.Label("married-true", "Yes") @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" }) @Html.Label("married-false", "No") </fieldset> 

You can add a @checked parameters the anonymous object like so to set the radio button as the default:

new { id = "married-true", @checked = 'checked' } 

Note that you can bind to a string by replacing true and false with the string values.