Validation Error: The value 'on' is not valid for <<property name>>

18,356

Solution 1

You should change OtherPlaceProvinceId from int to int?,

public int? OtherPlaceProvinceId { get; set; }

Solution 2

Is OtherPlace a checkbox? The default value for a checkbox is on if it's ticked & blank if it's not. The ModelBinder doesn't understand this.

ASP.Net deals with this, if you use the helpers, by doing this:

<input type="checkbox" name="OtherPlace" value="true"/>
<input type="hidden" name="OtherPlace" value="false"/>

The modelbinder will now figure out of the checkbox was ticked or not, convert it to a boolean & bind it to your model.

You could also use radio buttons with true/false values

Solution 3

I'll do some workaround when you don't have too many inputs with this behavior.
It's simple, before you submit the form, catch the inputs with the 'on' string and change it for the true/false value.

function toggle_validator(e) {
    if ($('#OtherPlace').val() == 'on') {
        $('#OtherPlace').val(true);
    }
    else {
        $('#OtherPlace').val(false);
    }
}

And we need to bind this event to the button that submit the form.

<input type="submit" value="Save" class="btn btn-success" onclick="toggle_validator();" />
Share:
18,356

Related videos on Youtube

user3206982
Author by

user3206982

Updated on September 16, 2022

Comments

  • user3206982
    user3206982 over 1 year

    In my project, I have a model that you can see part of my model here:

    public class CheckoutModel
    {
        public bool OtherPlace { get; set; }
    
        [RequiredIf("OtherPlace", true, ErrorMessage = " ")]
        public string OtherPlaceFullName { get; set; }
    
        [RequiredIf("OtherPlace", true, ErrorMessage = " ")]
        public int OtherPlaceProvinceId { get; set; }
    
        [RequiredIf("OtherPlace", true, ErrorMessage = " ")]
        public string OtherPlaceCity { get; set; }
    }
    

    I used RequiredIf attribute to validate my model in view,

     if (!ViewData.ModelState.IsValid)
        {
            @Html.ValidationSummary(false)
        }
    

    I fill all property of my form but I get below validation error when OtherPlaceProvinceId is not filled.

    The value 'on' is not valid for OtherPlace.

    UPDATE: The controller is here:

        [HttpGet]
        public ActionResult CheckoutAccount()
        {
            var model = OrderManager.Instance.GetCheckoutAccount();
            return View("_CheckoutAccount", model);
        }
    
        [HttpPost]
        public ActionResult CheckoutAccount(CheckoutAccountModel model)
        {
            return View("_CheckoutAccount", model);
        }
    
  • Simon Halsey
    Simon Halsey over 10 years
    This would only be needed if you can post a blank value for this field
  • frostymarvelous
    frostymarvelous over 7 years
    In asp core, I didn't need to add the hidden field.
  • Kappacake
    Kappacake almost 6 years
    This answer is pretty meaningless! How does it even relate to the problem mentioned in the answer??
  • robbpriestley
    robbpriestley over 5 years
    Or just use @Html.CheckBoxFor(model => model.OtherPlace) in the view
  • Simon Halsey
    Simon Halsey over 5 years
    yep, that's what I meant when I said "if you use the helpers"
  • Rob
    Rob about 3 years
    This got me close, however, in my testing, $('#OtherPlace').val() was equal to 'true' not 'on', but otherwise, this solution worked perfectly. Thanks.