Note that there are some explanatory texts on larger screens.

plurals
  1. POModelState is invalid when posting a form within checked checkbox
    primarykey
    data
    text
    <p>I'm experiencing following issue: if I submit a form that contains checked value in checkbox to the api controller via AJAX, ModelState object says it's invalid.</p> <p>Prerequisites:</p> <ol> <li>Visual Studio 2012</li> <li>ASP.NET MVC 4 Final</li> <li>Latest jQuery and jQuery unobtrusive validation stuff (versions 1.8.2 and 1.9.0.1)</li> </ol> <p>Steps to reproduce:</p> <ol> <li>Created a ViewModel for form, containing Boolean field: <code><pre> public class CheckboxViewModel { [Display(Name="Test checkbox")] public bool TestCheckbox { get; set; } } </pre></code></li> <li>Created controller action that simply prepares ViewModel and renders form: <code><pre> public ActionResult Index() { return View(new CheckboxViewModel()); } </pre></code></li> <li>Created a view with form <code><pre> @using (Ajax.BeginForm("Post", "api/Values", null, new AjaxOptions { HttpMethod = "POST" }, new { id = "form" })) { @Html.ValidationSummary(true) @Html.LabelFor(model => model.TestCheckbox) @Html.EditorFor(model => model.TestCheckbox) @Html.ValidationMessageFor(model => model.TestCheckbox) &lt;input type="submit" value="Submit" /&gt; } </pre></code></li> <li>Created Web API action that checks if modelstate is valid and return status code 200,otherwise 400: <code><pre> public HttpResponseMessage Post(CheckboxViewModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); return new HttpResponseMessage(HttpStatusCode.OK); } </pre></code></li> </ol> <p>I've done some googling and I know that editor for checkbox renders additional hidden field, but it seems to be ok and is needed for model binding. So, when I uncheck checkbox and submit form everything works fine, server responds with status 200, thanks to hidden field, I suppose. However, when I make checkbox checked and submit form, server responds with status 400, meaning that ModelState is in invalid state. ModelState in this case contains error with empty ErrorMessage and following Exception message: <code><pre> {System.InvalidOperationException: The parameter conversion from type 'System.Collections.Generic.List`1[System.String]' to type 'System.Boolean' failed because no type converter can convert between these types. в System.Web.Http.ValueProviders.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType) в System.Web.Http.ValueProviders.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType) в System.Web.Http.ValueProviders.ValueProviderResult.ConvertTo(Type type, CultureInfo culture) в System.Web.Http.ValueProviders.ValueProviderResult.ConvertTo(Type type) в System.Web.Http.ModelBinding.Binders.TypeConverterModelBinder.BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)} </pre></code></p> <p>I have no idea how to handle this properly. Should I create custom model binder? Or am I missing something? Any help would be appreciated. Thanks.</p> <p>I've created <a href="https://github.com/hjarvard/Checkbox-WebAPI-Test" rel="nofollow">solution</a> that reproduces my issue.</p> <p><strong>UPDATE:</strong> I've figured out that form actually contains values <code>TestCheckbox: true</code> and <code>TestCheckbox:false</code>, so I think this maybe some kind affects binder and it throws an exception. Still have no options how to workaround this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload