52

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,,,,

1
  • 3
    Set your model property to true Commented May 8, 2013 at 7:57

8 Answers 8

82

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to checked without changing model.as value in Controller.. how to change value in view
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.
i tried with @Html.CheckBoxFor(model => model.As, new { @checked = "true" }) but its not working
Yes, it's not working because the helper doesn't support this attribute. It uses the value of the As property on your model which, as I already explained in my answer, should be set in your controller action. But I repeat once again, if you don't want to follow this recommended approach you should hardcode your checkbox manually: <input type="checkbox" name="As" value="true" checked="checked" /><input type="hidden" name="As" value="false" />.
After changing model.as value in controller its affecting other view, that's why i want to set in that particular view,
55

Old question, but another "pure razor" answer would be:

@Html.CheckBoxFor(model => model.As, htmlAttributes: new { @checked = true} )

5 Comments

This has no effect if the model value is "false" (the checkbox is rendered unchecked) and is not needed if the model value is "true," so what is the point of this code?
What about when it's null? (bool ?)
It's indeed useful on Create New view. If you want to create or add new data, sometimes you want a default value. For example field for "Marital Status" - maybe you want to set "Yes" or "Checked" as default value on the form.
This does not model bind properly and was the root of a bug I was just working on (.net framework 4.6 at least)...This should not have this many upvotes
That being said, this can be leveraged for a hacky solution if your architecture is bad enough that you cannot quickly do Darin's (the approved) answer. You can use this line in conjunction with defaulting the backing boolean property to true public bool As { get; set; } = true; but I pray you are not working with spaghetti code like I am...
3

You could set your property in the model's constructor

public YourModel()
{
    As = true;
}

Comments

3
@Html.CheckBox("yourId", true, new { value = Model.Ischecked })

This will certainly work

Comments

0

An alternative solution is using jQuery:

    <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            PrepareCheckbox();
        });
        function PrepareCheckbox(){
            document.getElementById("checkbox").checked = true;
        }
    </script>

Comments

0

I use viewbag with the same variable name in the Controller. E.g if the variable is called "IsActive" and I want this to default to true on the "Create" form, on the Create Action I set the value ViewBag.IsActive = true;

public ActionResult Create()
{
    ViewBag.IsActive = true;
    return View();
}

Comments

0

My way is @Html.CheckBoxFor(model => model.As, new { @value= "true" }) (meaning is checked)

Comments

0

In razor, set value first.

@{model.As = true;}
@Html.CheckBoxFor(model => model.As)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.