0

I have the following ViewModel

public class ReportFilters
{
    [Required]
    public DateTime From { get; set; }

    [Required]
    public DateTime To { get; set; }

    [Required]
    public int UserId { get; set; }

    public bool IsCorrect { get; set; }
} 

I pass either the value 1 or 0 when I call this route to IsCorrect but the ModelState.IsValid is always returning false.

This started to happen after I added public bool IsCorrect { get; set; }

Here is how my controller action look like

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult GetTotals([Bind(Include = "From,To,UserId,IsCorrect ")] ReportFilters reportFilters)
{
    if (ModelState.IsValid)
    {
        SomeModel results;

        if(reportFilters.IsCorrect)
        {
            results = conn.Database.SqlQuery<SomeModel>(SomeQuery, from, to).ToList();
        } else 
        {
            results = conn.Database.SqlQuery<SomeModel>(SomeOtherQuery, from, to).ToList();
        }

        return Json(results, JsonRequestBehavior.AllowGet);
    }

    return Json(new { }, JsonRequestBehavior.AllowGet);

}

Question

How can I correctly validate the value of a check box? If the value is 1 then it should be true otherwise false.

1
  • Could you take out [Bind(Include = "From,To,UserId,IsCorrect ")], and try again? Commented Jul 25, 2016 at 21:36

1 Answer 1

2

Ensure You Are Passing Your Parameters Properly

Have you tried passing true or false explicitly (i.e. Controller/Action?IsCorrect=true) as opposed to a 0 or 1?

A checkbox should always bind directly to a boolean value. You'll just need to ensure that the name attribute of your checkbox matches your parameter or model property exactly (i.e. IsCorrect) :

<!-- Using a plain CheckBox -->
<input name='IsCorrect' type='checkbox' />
<!-- Using an HTML Helper -->
@Html.CheckBox("IsCorrect")

You can see a working example demonstrating this below :

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

using the value true and false worked. but if I pass false the the ModelState.IsValid will also return false. The check box should be optional and not required
There isn't anything within your model that would cause it to be invalid just because of the IsCorrect property. However, you'll need to make sure that you pass along the other properties as those are decorated with [Required] tags and thus will cause validation errors if empty / null.
I am positing using ajax request via jQuery something like this ` if ($('#CorrectDispositions').is(':checked')) return "true"; else return "false";`
The is() function will return a boolean value, so if you are planning on passing that, you might explicitly indicate that it is bound to your IsCorrect property (i.e. { /* Other data here */ , IsCorrect : $('#CorrectDispositions').is(':checked') })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.