开发者

ASP.NET MVC 3 CheckboxFor retains previous value, despite Model value

开发者 https://www.devze.com 2023-04-11 21:36 出处:网络
I\'m attempting to add a classic Accept Terms and Conditions checkbox on the log on page of an MVC application.

I'm attempting to add a classic Accept Terms and Conditions checkbox on the log on page of an MVC application.

If the user accepts the Terms and Conditions, but fails to log on for some other reason (bad password etc), then I want the Accept T&Cs checkbox not to be checked, so the user is forced to accept the T&Cs on every log on attempt.

The problem 开发者_Go百科is that using Html.CheckboxFor(), after a postback the checkbox retains its previous value, despite the value of the bound Model property.

Here's the code, stripped down to essentials. If you run this code up, check the checkbox, and click the button, you'll be returned to the form with the checkbox still checked, even though the bound model property is false.

The Model:

namespace Namespace.Web.ViewModels.Account
{
    public class LogOnInputViewModel
    {
        [IsTrue("You must agree to the Terms and Conditions.")]
        public bool AcceptTermsAndConditions { get; set; }
    }
}

The validation attribute:

public class IsTrueAttribute : ValidationAttribute
{
    public IsTrueAttribute(string errorMessage) : base(errorMessage)
    {
    }

    public override bool IsValid(object value)
    {
        if (value == null) return false;
        if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
        return (bool)value;
    }
}

The View:

@model Namespace.Web.ViewModels.Account.LogOnInputViewModel

@using (Html.BeginForm()) {
    @Html.CheckBoxFor(x => x.AcceptTermsAndConditions)
    <input type="submit" value="Log On" />
}

The Controller:

    [HttpGet]
    public ActionResult LogOn(string returnUrl)
    {
        return View(new LogOnInputViewModel { AcceptTermsAndConditions = false });
    }

    [HttpPost]
    public ActionResult LogOn(LogOnInputViewModel input)
    {
        return View(new LogOnInputViewModel { AcceptTermsAndConditions = false });
    }

I saw the suggestion on asp.net to add a @checked attribute to the CheckboxFor. I tried this, making the view

@model Namespace.Web.ViewModels.Account.LogOnInputViewModel

@using (Html.BeginForm()) {
    @Html.CheckBoxFor(x => x.AcceptTermsAndConditions, new { @checked = Model.AcceptTermsAndConditions })
    <input type="submit" value="Log On" />
}

And I saw the same behaviour.

Thanks for any help/insights!

Edit: Although I want to override the posted back value, I wish to retain the message if validation of AcceptTermsAndConditions fails (there is a validation attribute on AcceptTermsAndConditions requiring it to be true), so I can't use ModelState.Remove("AcceptTermsAndConditions") which was the otherwise sound answer @counsellorben gave me. I've edited the code above to include the validation attribute - apologies to @counsellorben for not being clearer originally.


You need to clear the ModelState for AcceptTermsAndConditions. By design, CheckBoxFor and other data-bound helpers are bound first against the ModelState, and then against the model if there is no ModelState for the element. Add the following to your POST action:

ModelState.Remove("AcceptTermsAndConditions");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号