I am new to JavaScript and currently working on a view where the submit button should save data. If the textboxes are empty, there should be an error message displayed using Bootstrap alert. However, in all cases, only the success alert pops up, even when the fields are empty. Any help would be greatly appreciated.
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function ()
{
$("#btnSubmit").click(function () {
var data = $("#myForm").serialize();
var newUrl = '@Url.Action("Index","AspNetUsers")';
$.ajax({
type: "POST",
url: "/AspNetUsers/Index1",
data: data,
success: function (response) {
$('#your-modal').fadeTo(2000, 500).slideUp(500, function () {
$('#your-modal').slideUp(500);
window.location.href = newUrl;
});
},
error: function (response) {
$('#your-modal2').fadeTo(2000, 500).slideUp(500, function () {
$('#your-modal2').slideUp(500);
});
}
})
})
})
</script>;
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<style>
#your-modal
{
text-align:center;
}
#your-modal2
{
text-align:center;
}
#x:hover {
padding: 15px;
border: 1px solid #6699ff;
border-bottom-width: 2px;
background-color: transparent;
}
.col-md-6.active:hover {
box-shadow: 2px 2px 2px 2px rgba(4,4,4,.4) !important;
}
</style>
@model CMSFC.Models.AspNetUser
@{
ViewBag.Title = " ";
Layout = "~/Views/Shared/_Layout.cshtml";
}
...
e // GET: AspNetUsers/Create
public ActionResult Index1()
{
return View();
}
// POST: AspNetUsers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index1([Bind(Include = "Id,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] AspNetUser aspNetUser)
{
if (ModelState.IsValid)
{
db.AspNetUsers.Add(aspNetUser);
db.SaveChanges();
// return Json("User Details are updated");
return RedirectToAction("Index");
}
return View(aspNetUser);
}