I have several checkboxes on a form that is loading as a model in a view model. Everything is working as planned if the checkbox is checked, but causes a clientside validation error if I leave it unchecked. I want the user to be able to choose NOT to check it. I have a small easy to digest version here. There is the hidden input with the value of false that should do the trick if there is no form submission of no action taken/ unchecked box, but I cannot seem to get this to submit when there is more than one model in the view model. Is there a trick to posting back with checkboxes and multiple models in the view? Am I just missing a parameter for the checkbox? I have @Html.EditorFor working in many pages, it is the scaffolded code, however, I did try @Html.CheckBox and @Html.CheckBoxFor and all the same result. Clientside is not valid and will not let the form submit. I don't know everything, and I can see that the dat-val-required attribute has a message, I don't know exactly how that gets there, but I need it to not be required. Thank you so much for any help you can offer!
public partial class Application
{
public Boolean Deny_Insufficient_Prep { get; set; }
}
public class PageBuilder
{
public Boolean Deny_Insufficient_Prep { get; set; }
}
public class ApplicationScoringViewModel
{
// Collection of input configurations to include on a given page
public PageBuilder PageBuilder { get; set; }
// Model class that knows all fields that can be chosen
public Application Application { get; set; }
}
public ActionResult RecordApplicationData(int id, int appId)
{
ApplicationScoringViewModel appVm = new ApplicationScoringViewModel
{
Application = db.Application.Find(id),
PageBuilder = db.PageBuilder.Find(appId),
};
return View(appVm)
}
@model ProjectNamespace.Models.ViewModels.ApplicationScoringViewModel
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.Application.Deny_Insufficient_Prep, new { htmlAttributes = new { @class = "form-control ignore" } })
@Html.ValidationMessageFor(model => model.Application.Deny_Insufficient_Prep, "", new { @class = "text-danger" })
}
Renders to:
<form action="/RecordApplicationData?id=123&appId=456" method="post">
<input checked="checked" class="form-control ignore check-box" data-val="true" data-val-required="The Unsatisfactory scholarship field is required." id="Application_Deny_Insufficient_Prep" name="Application.Deny_Insufficient_Prep" type="checkbox" value="true" />
<input name="Application.Deny_Insufficient_Prep" type="hidden" value="false" />
<span class="field-validation-valid text-danger" data-valmsg-for="Application.Deny_Insufficient_Prep" data-valmsg-replace="true"></span>
<input type="submit" id="submit" value="Save Score" class="btn btn-primary" />
</form>
Aucun commentaire:
Enregistrer un commentaire