I use AJAX to send forms to controller in ASP.NET Core but I have problem with send form with validation
<form asp-action="Create" asp-controller="Departments"
data-ajax="true"
data-ajax-method="Post"
data-ajax-mode="replace"
data-ajax-update="#content"
data-ajax-success="Success"
data-ajax-failure="Failure">
<div class="box-body">
<div class="alert alert-success" id="divalert" ></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="ثبت" class="btn btn-success" />
</div>
</div>
</form>
This is Jquery code For Ajax
<script>
function Success() {
$("#divalert").text = "Yes";
}
function Failure() {
$("#divalert").text = "No";
}
</script>
but I want to show validation message ehen I send form with empty text Box ,
This is my Controller
public async Task<IActionResult> Create([Bind("Department_Id,Title,Task,Description,Department_Status")] Department department)
{
if (ModelState.IsValid)
{
_genericRepository.Add(department);
await _genericRepository.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(department);
}
How do I show validation message with Ajax when form is empty ?
jquery.unobtrusive-ajax.jsplugin (which makes an ajax call)[Required]attribute on theTitleproperty. Do you have all the relevant scripts loaded correctly?