I am using CheckBox in my ASP.Net MVC project,
i want to set checkBox by default checked,
My CheckBox is
@Html.CheckBoxFor(model => model.As, new { @checked = "checked" })
but its not working,,,,
The correct way to achieve that is to set the value in the controller. That's how the CheckBoxFor helper is designed to be used. If you don't want to follow the best practices you could always manually generate the checkbox in the view with a hardcoded checked="checked" attribute.
Hi akhter, Set the CheckBox Checked property to true and Enabled property to false. This will set default check on all rows in GridiVew and can not uncheck CheckBox.
Razor offers two ways to generate checkboxes. The recommended approach is to use the input tag helper. Any boolean property of the PageModel will render a checkbox if it is passed to the asp-for attribute, so long as the property is not nullable: public class IndexModel : PageModel.
In your controller action rendering the view you could set the As
property of your model to true:
model.As = true; return View(model);
and in your view simply:
@Html.CheckBoxFor(model => model.As);
Now since the As property of the model is set to true, the CheckBoxFor helper will generate a checked checkbox.
Old question, but another "pure razor" answer would be:
@Html.CheckBoxFor(model => model.As, htmlAttributes: new { @checked = true} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With