4

I am working on an ASP.net MVC project and I am using the CheckBoxFor helper method to provide the value for a boolean in my model like so:

@Html.CheckBoxFor(m => m.ShouldSaveSearch, new { id="ShouldSaveSearch"})

If the checkbox gets checked by the user, it works completely fine and when the model is received by the controller. The ShouldSaveSearch property will be set to true.

[HttpGet]
public ActionResult Search(int studentSearchId = -1)
{
    return View(new StudentSearchModel(studentSearchId));
}

[HttpPost]
public ActionResult Search(StudentSearchModel m)
{
    ViewBag.SearchResults = Hub.Web.Models.Student.StudentSearchModel.Search(m);
    Hub.Web.Models.Student.StudentSearchModel.Save(m);
    m.ShouldSaveSearch = false;
    m.ShouldShareSearch = false;
    m.SavedSearchName = "";
    m.SavedSearchDescription = "";

    return View(m);
}

I then set the ShouldSaveSearch property to false and then return the same view with the same model that was originally submitted.

However, when the view renders, the checkbox for this property remains checked. Is there something that I am missing that is preventing the checkbox from unchecking itself?

2
  • 1
    It looks like you are resetting the form and then re-displaying the view. A better practice (the post-redirect-get pattern) would be to RedirectToAction("Search")<--targeting the GET request for the view instead of the POST
    – Forty-Two
    Commented Feb 28, 2013 at 21:14
  • 1
    You don't need the id = "ShouldSaveSearch" - that would be generated by mvc itself. Second, where is the [HttpPost] attribute?
    – codingbiz
    Commented Feb 28, 2013 at 21:14

3 Answers 3

2

You seem to want to default the checkbox to unchecked. That is Not linked to the true/false value of lambda but the value of checked attribute

@Html.CheckBoxFor(m => m.ShouldSaveSearch, new { @checked="false"})

The other's points are equally important. Clear the ModelState!

1

It sounds like the ModelState problem discussed in this question and also here

In a few words the HtmlHelper displays ModelState value not Model. See question for more details.

Possible options:

  • Implement the post-redirect-get pattern as suggested by Forty-Two
  • Reset the checkbox value using the ModelState collection, something like ModelState["ShouldSaveSearch"].Value = false
3
  • This was indeed my problem, however, clearing the ModelState would remove other values from it that I do not want to be removed, and attempting the ModelState["ShouldSaveSearch"].Value = false solution returns an error "Cannot implicitly convert type 'bool' to 'System.Web.Mvc.ValueProviderResult'".
    – bjonnn
    Commented Feb 28, 2013 at 21:31
  • I was able to remove the one value by ModelState["ShouldSaveSearch"].Value = new ValueProviderResult("", "", CultureInfo.CurrentCulture);.
    – bjonnn
    Commented Feb 28, 2013 at 21:34
  • As a further update, I was able to simplify it by saying m.ShouldSaveSearch = false; and then ModelState.Remove("ShouldSaveSearch");
    – bjonnn
    Commented Mar 5, 2013 at 14:58
1

The problem here is that if you return the same view to which you posted with the model data, MVC will think that you are returning because of an error. This is a normal behavior. If you want to redisplay the view then you should implement PRG pattern (Post-Redirect-Get). You issue will be always there regardless of whether ModelState.IsValid is true or false. You should redirect to the HttpGet version of your view, passing the parameter, and loading the data. If you want to avoid loading, store the data in TempData or some other Session implementation.

UPDATE: You are calling the version of an action that receives model object. Once you persist data call

return RedirectToAction("Search", new {studentSearchId = your_value});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.