'The record is getting inserted or updated in datatable after page refresh and also notify.js popup is not working after insert/update in ASP.NET MVC
I'm new to ASP.NET MVC and I have created CRUD operations using datatables.
I need some help for notify.js popup is not working after inserting and updating data and also the record is getting inserted or updated in datatable after page refresh in ASP.NET MVC.
This is my markup and code:
AddOrEdit.cshtml:
@model Asp.NETMVCCRUD.Models.Employee
@{
Layout = null;
}
@using (Html.BeginForm("AddOrEdit", "Employee", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))
{
@Html.HiddenFor(model => model.EmployeeID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { autocomplete = "off", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Position, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Position, new { htmlAttributes = new { autocomplete = "off", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Position)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Office, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Office, new { htmlAttributes = new { autocomplete = "off", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Office)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Age, new { htmlAttributes = new { autocomplete = "off", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Salary, new { htmlAttributes = new { autocomplete = "off", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Salary)
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary" />
<input type="reset" value="Reset" class="btn btn-warning" />
</div>
}
Index.cshtml:
<script>
function SubmitForm(form) {
debugger
$.validator.unobtrusive.parse(form);
if ($(form).valid) {
$.ajax({
type: "post",
url: form.action,
data: $(form).serialize(),
success: function (data) {
if (data.success) {
Popup.dialog('close');
dataTable.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
})
}
}
});
}
return false;
}
</script>
_Layout.cshtml:
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery-ui-1.13.1.min.js"></script>
<script src="~/Scripts/notify.min.js"></script>
EmployeeController.cs:
[HttpPost]
public ActionResult AddOrEdit(Employee emp)
{
using (DBModel db = new DBModel())
{
if (emp.EmployeeID == 0)
{
db.Employees.Add(emp);
db.SaveChanges();
return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
else
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
