I have a form that when it validates its breaking my javascript I use to check boxes.
Here is the form
@using (Html.BeginForm("HomeEquityFinal", "Home", FormMethod.Post))
{
<div class="row">
<div class="col-md-offset-4">
<div class="checkbox disabled">
<label>
@Html.CheckBoxFor(m => m.Terms, new { id = "termsCheck", disabled = "disabled" }) I agree to the <a data-toggle="modal" data-target="#termsModal">Terms and Conditions</a><br />
@Html.ValidationMessageFor(x => x.Terms)<br/>
@Html.CheckBoxFor(m => m.Terms1, new { id = "termsCheck1", disabled = "disabled" }) I agree to the <a data-toggle="modal" data-target="#termsModal1">Terms and Conditions</a><br />
@Html.ValidationMessageFor(x => x.Terms1)
</label>
</div>
</div>
</div>
}
This is the javascript
$('#confirm').click(function () {
$("#termsModal").modal('hide');
$("#termsCheck").attr('checked', 'checked');
return false;
});
$('#confirm1').click(function () {
$("#termsModal1").modal('hide');
$("#termsCheck1").attr('checked', 'checked');
return false;
});
My validation is done in the model using this
[Range(typeof(bool), "true", "true", ErrorMessage = "Please agree to the terms and conditions.")]
public bool Terms { get; set; }
[Range(typeof(bool), "true", "true", ErrorMessage = "Please agree to the terms and conditions.")]
public bool Terms1 { get; set; }
I believe the placement of the !modelstate.isvalid in the controller is causing issues. The form pulls everything through in session.
This is part of my controller
var notes = model.HomeEquityDetails.Notes;
model = Session["HomeEquityConfirmation"] as HomeEquityConfirmationViewModel;
if (!ModelState.IsValid)
{
model = Session["HomeEquityConfirmation"] as HomeEquityConfirmationViewModel;
return View(model);
}
So after I submit the form and it does its validation properly I can no longer click my confirm button and get the box to check and move on to the next page. It just keeps posting back saying the fields are required. In the code it has a hidden field marked false. If i change that to true in developer tools I can move on.
THe main issue is the breaking of the javascript when posting back.
Aucun commentaire:
Enregistrer un commentaire