vendredi 14 décembre 2018

ASP.Net MVC checkbox using prettycheckable keeps checking after validation fails and returns to view

I'm not sure if this is standard behavior but I have a checkboxfor in my form and I'm using the prettycheckable JS lib to style it. When I post to the server and the model state is invalid and I return to the page, the checkbox is still checked!

I try to set the models property bool value to false in the action, but I still have it checked when the page returns. I also tried setting 'checked = false' in the element but that didn't do anything either.

Is this what I should expect? I would like to reset it and have it unchecked if the model state fails and I have to return to the page.

Here is the checkbox

@Html.CheckBoxFor(m => m.AgreeToTerms, new { @id = "AgreeToTerms", data_label = "Agree To Terms", @checked = false })

Here is the viewmodel

public bool AgreeToTerms { get; set; }

in my action here I try and set the checkbox to false if the model state fails

    [HttpPost]
    public ActionResult SomeAction(SomeViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            viewModel.AgreeToTerms = false;
            return View("SomeView", viewModel);
        }
    } 




Aucun commentaire:

Enregistrer un commentaire