views:

84

answers:

2

I have a simple model FilesModel for updating a string Description and the boolean value of a checkbox Archived for a few (already uploaded) files, and FilesModel has a validator FilesModelValidator that gets run when this data is posted. This validator does nothing more than check that each file has a description. I know that it runs and correctly returns an error for empty descriptions based on my debugging so far.

However, when control is given to the Action method in the Controller, ModelState is different from what I expect. There are no errors on the description fields, but there is one error for each checkbox that is checked: "The value 'on' is not valid for Archived."

Validation of this sort works just fine in other areas of the site, so I'm sure there's some minute thing I'm overlooking. Any suggestions as to why this may be happening and how to fix it?

Validator

public FilesModelValidator()
{
    RuleFor(f => f.Files)
        .Must(AllHaveADescription).WithMessage("Must have a description");
}

public static bool AllHaveADescription(Files files)
{
    // This is run on postback, and returns false when any Description is empty
    return files.All(f => f.Description != null && f.Description.Length > 0);
}

Controller

[HttpPost]
public virtual ActionResult Update(FilesModel model)
{
    // At this point, ModelState contains an error for each checked checkbox
    // and no errors for empty descriptions
    if (ModelState.IsValid)
    {
        // Save
    }
    return View(model);
}
A: 

I had the same issue for items in a drop down that were linked to an Enum. I had stripped out the default value and so when someone didn't select something I got an error.

So, having said that, I think your problem might be kinda linked in that the binder is looking for a true/false when your model is expeting a yes/no or vice versa.

Two ways around this might be to change your view to be True/False or, and this is what I did, to write my own ModelBinder which does the conversion from Yes to True.

I hope this is of some help to you.

griegs
I have a custom model binder which correctly interprets the value of the checkbox, and the boolean on the model is correctly set to true or false. It appears that it's not the model binding that is at fault, but a validator that I'm not seeing.
Aaron
A: 

It turns out the checkbox thing was the entire problem. I found a solution to this problem elsewhere in our code, so I used it. It seems kind of hacky, but it works.

The idea is that you need to make sure that the checkbox's value is true and not "on". So do this:

<input type="checkbox" id="myCheckbox" value="true" />

Then add a hidden input with the same id with its value as false immediately after the checkbox:

<input type="hidden" id="myCheckbox" value="false" />

When a checkbox is not checked, the checkbox value does not get posted back to the server. So when the postback occurs, the server sees myCheckbox=false which is exactly what we would want in this case. When the checkbox is checked, both input values get posted to the server. But the server uses only the first value (which is the value of the checkbox itself, since we put it before the hidden field). So the server sees myCheckbox=true.

Aaron