Good day to all ASP.Net users,
could you please help me, what problem with my code edit and delete button not popup or nothing happen,
and Add New button validation is not working and also notify is not working once I saved data, could you please help regarding my problem , please refer my code below, thank you.
Index
@{
ViewBag.Title = "Department List";
}
<h2 class="w3-content"><b>Departments</b></h2><p class="w3-blockquote w3-content"> List of Departments</p><div class="w3-content"><a class="btn btn-success" style="margin-top:10px;" onclick="Popupform('@Url.Action("AddorEdit","Department")')"><span class="glyphicon glyphicon-plus"></span> Add New</a></div><div class="col-md-12"><br /></div><div class="w3-content"><table id="departmenttable" class="table table-striped table-bordered" ><thead><tr><tr class=""><th class="w3-text-black"><b>DEPARTMENTID</b></th><th class="w3-text-black"><b>DEPARTMENTNAME</b></th><th class="w3-text-black"><b>REQUESTLIMIT</b></th><th></th></tr></thead></table></div><div class="col-md-12"><br /></div><div class="col-md-12"><br /></div><link href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css" rel="stylesheet" />
@section scripts{><script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script><script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script><script src="~/Scripts/notify.min.js"></script><script>
var Popup, dataTables;$(document).ready(function(){
dataTables = $('#departmenttable').dataTable({"ajax": {"url": "/Department/GetData","type": "GET","datatype" : "json"
},"columns":
[
{ "data": "DEPARTMENTID" },
{ "data": "DEPARTMENTNAME" },
{ "data": "REQUESTLIMIT" },
{"data": "DEPARTMENTID", "render": function (data) {
return "<a class='btn btn-default btn-sm' onclick='Popupform('@Url.Action("AddorEdit","Department")/" + data + "')><span class='glyphicon glyphicon-edit'></span> Edit</a> <a class='btn btn-danger btn-sm' onlclick=Delete("+ data +")><span class='glyphicon glyphicon-remove'></span> Delete</a>";
},
"orderable": false,"searchable": false
}
],
"language": {"emptyTable" : "No data found, Please click on <b>Add New</b> button"
}
});
});
function Popupform(url) {
var fromDiv = $('<div/>');$.get(url)
.done(function (response) {
fromDiv.html(response);
Popup = fromDiv.dialog({
autoOpen: true,
resizable: false,
title: 'Fill Department Details',
width: 400,
height: 350,
close: function () {
Popup.dialog('destroy').remove();
}
});
});
}
function Submitform(form) {
$.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');
dataTables.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className : "success"
});
}
}
});
}
return false;
}
function Delete(id)
{
if (confirm('Are you sure to Delete this Employee Record ? '))
{
$.ajax({
type: "POST",
url: '@Url.Action("Delete","Department")/' + id,
success: function (data) {
if (data.success) {
dataTables.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
})
}
}
});
}
}
</script>
}AddorEdit.cshtml
@model WarehouseRtoRSystem.Models.DepartmentModel
@{
Layout = null;
}
@using (Html.BeginForm("AddorEdit", "Department", FormMethod.Post, new {onsubmit = "return Submitform(this)" }))
{<div class="form-group">
@Html.LabelFor(model => model.DEPARTMENTID, new { @class = "control-label col-md-2" })<div class="col-md-12">
@Html.TextBoxFor(model => model.DEPARTMENTID, null, new { @class = "form-control" })</div></div><br /><div class="form-group">
@Html.LabelFor(model => model.DEPARTMENTNAME, new { @class = "control-label col-md-2" })<div class="col-md-12">
@Html.TextBoxFor(model => model.DEPARTMENTNAME, null, new { @class = "form-control" })</div></div><br /><div class="form-group">
@Html.LabelFor(model => model.REQUESTLIMIT, new { @class = "control-label col-md-2" })<div class="col-md-12">
@Html.TextBoxFor(model => model.REQUESTLIMIT, null, new { @class = "form-control", type = "number" })</div></div><div class="col-md-10"><br /></div><div class="form-group"><div class=" col-md-12"><input type="submit" value="Create" class="btn btn-primary" /><input type="reset" value="Reset" class="btn btn-default" /></div></div>
}Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WarehouseRtoRSystem.Models;
namespace WarehouseRtoRSystem.Controllers
{
public class DepartmentController : Controller
{
private readonly DepartmentContext DepartmentDb = new DepartmentContext();
//
// GET: /Department/
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
var deptlist = DepartmentDb.List();
return Json(new { data = deptlist }, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult AddorEdit(string id)
{
if (id == null)
return View(new DepartmentModel());
else
{
return View(DepartmentDb.List().Where(i => i.DEPARTMENTID == id).FirstOrDefault());
}
}
[HttpPost]
public ActionResult AddorEdit(DepartmentModel department)
{
if (department.DEPARTMENTID != null)
{
DepartmentDb.SaveAdd(department);
return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
else
{
DepartmentDb.SaveUpdate(department);
return Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet);
}
}
[HttpPost]
public ActionResult Delete(string id)
{
DepartmentModel dept = DepartmentDb.List().Where(d => d.DEPARTMENTID == id).FirstOrDefault();
DepartmentDb.SaveDelete(dept.DEPARTMENTID.Trim());
return Json(new { success = true, message = "Deleted Successfully" }, JsonRequestBehavior.AllowGet);
}
}
}Model
using System.ComponentModel.DataAnnotations;
namespace WarehouseRtoRSystem.Models
{
public class DepartmentModel
{
[Required(ErrorMessage="This Fiels is Required")]
public string DEPARTMENTID { get; set; }
[Required(ErrorMessage = "This Fiels is Required")]
public string DEPARTMENTNAME { get; set; }
public int GSEXPNCBUDGET { get; set; }
public int GSBUDGET { get; set; }
public int PIECEPARTBUDGET { get; set; }
public int REQUESTLIMIT { get; set; }
}
}