mishap
mishap

Reputation: 8505

MVC checkbox needs to be unchecked

I have a few checkboxes values of which are posted to action, and when action calls the view again the checkboxes are checked according to their previous state:

<div class="D2">@Html.CheckBox("int", false, new { id = "int" })</div>
<div class="D2">@Html.CheckBox("ext", false, new { id = "ext" })</div>
<div class="D2">@Html.CheckBox("none", false, new { style = "visibility:hidden", id = "none" }
</div>

Action:

public ActionResult Images(bool? int, bool? ext, bool? none)
return View();

But I want the third theckbox ALWAYS to be unchecked, while the other ones should keep the settings. How can I achieve that please?

Upvotes: 1

Views: 1241

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

If you want it to always be unchecked, why even send it into the method, simply use a value of false in your ActionResult. It doesn't have to be present in the parameter list. Am I not following something here?

Upvotes: 0

Scott Rippey
Scott Rippey

Reputation: 15810

The Html helper methods ALWAYS get the value from ModelState if they can.
To ensure the third checkbox will be unchecked, you should clear the ModelState value in your controller:

public ActionResult Images(bool? int, bool? ext, bool? none) {
    ModelState.Remove("none");
    return View();
}

For more info, see ModelState.Remove.

Upvotes: 1

Related Questions