I want to implement server and client side validation in modal partial view but this validation is not working even If submit the textbox and Link blank. I have included bundle
into my main view under @section scripts{
. Currently, when i click on Save
, it saves the modal data even though the fields are empty.
Modal
[Required(ErrorMessage ="Please enter Title")]
public string Title { get; set; }
[Required(ErrorMessage = "Please enter valid URL link!")]
[Url(ErrorMessage ="Please enter valid URL link!")]
public string Link { get; set; }
Modal partial View
Note the form is loaded using ajax
<div class="modal fade" id="EditModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
@using (Html.BeginForm("_pEdit", "Home", FormMethod.Post, new { Id = "Editform"}))
{
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Link, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Link, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Link, "", new { @class = "text-danger" })
</div>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="submit-modal" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
Script
$(document).on("click", '.LinkId', function (e) {
$(this).load(actionURL, function (html) {
// Reparse the validator
var form = $('form');
form.data('validator', null);
$.validator.unobtrusive.parse(form);
$.ajax({
url: $(this).data("url"),
type: "GET",
success: function (response) {
if (response) {
$('#EditModal').replaceWith(response);
$('#EditModal').modal('show');
}
else {
var message = response.message;
alert(message);
}
}
});
return false;
})
});