'Getting Failed to load Resource: the sever responded with a status 500 ()
Using a simple MVC5 CRUD web app using razor and jquery to popup div window to add and update records connected to SQLserver via ado.net EF6. I have one view and controller working fine. The issue happens when I copied and updated a new view and controller. Please see the code of the view and controller and thank you for any assistance on this issue.
View:Index.cshtml
@{
ViewBag.Title = "Dept List";
}
<h2>Department</h2>
<a class="btn btn-success" style="margin-bottom:10px" onclick="PopupForm('@Url.Action("AddOrEdit","Dept")')"><i class="fa fa-plus"></i> Add New</a>
<table id="deptTable" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Fax</th>
<th>Loc</th>
<th>Address</th>
<th>Stop</th>
<th>URL</th>
<th></th>
</tr>
</thead>
</table>
<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
@section scripts{
<script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>
<script>
var Popup, dataTable;
$(document).ready(function () {
dataTable = $("#deptTable").DataTable({
"ajax": {
"url": "/Dept/GetData",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "Name" },
{ "data": "Phone" },
{ "data": "Fax" },
{ "data": "Location" },
{ "data": "Address" },
{ "data": "MailStop" },
{ "data": "URL" },
{"data":"DeptID" , "render" : function (data) {
return " <a class='btn btn-default btn-sm' onclick=PopupForm('@Url.Action("AddOrEdit","Dept")/" + data + "')><i class='fa fa-pencil'></i> Edit </a><a class='btn btn-danger btn-sm' style='margin-left:5px' onclick=Delete(" + data +")><i class='fa fa-trash'></i> Delete</a>";
},
"orderable": false,
"searchable":false,
"width":"150px"
}
],
"language": {
"emptyTable" : "No data found, Please click on <b>Add New</b> Button"
}
});
});
function PopupForm(url) {
var formDiv = $('<div/>');
$.get(url)
.done(function (response) {
formDiv.html(response);
Popup = formDiv.dialog({
autoOpen: true,
resizable: false,
title: 'Add New Department Details',
height: 400,
width: 700,
close: function () {
Popup.dialog('destroy').remove();
}
});
});
}
PopUp Div
AddOrEdit.cshmtl
@model Deparment.Models.tblDept
@{
Layout = null;
}
@using (Html.BeginForm("AddOrEdit", "Dept", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))
{
@Html.HiddenFor(model => model.DeptID)
<div class="form-group">
@Html.LabelFor(model => model.Dept, new { @class = "control-label" })
@Html.EditorFor(model => model.Dept, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DeptName)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, new { @class = "control-label" })
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Fax, new { @class = "control-label" })
@Html.EditorFor(model => model.Fax, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, new { @class = "control-label" })
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, new { @class = "control-label" })
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.MailStop, new { @class = "control-label" })
@Html.EditorFor(model => model.MailStop, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.URL, new { @class = "control-label" })
@Html.EditorFor(model => model.URl, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary" />
<input type="reset" value="Reset" class="btn" />
</div>
}
Controller:
namespace Department.Controllers
{
public class DeptController : Controller
{
//
// GET: /Dept/
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
using (DBModel db = new DBModel())
{
List<tblDept> deptList = db.tblDepts.ToList<tblDept>();
return Json(new { data = deptList }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddOrEdit(int id = 0)
{
if (id == 0)
return View(new tblDept());
else
{
using (DBModel db = new DBModel())
{
return View(db.tblDepts.Where(x => x.DeptID==id).FirstOrDefault<tblDept>());
}
}
}
[HttpPost]
public ActionResult AddOrEdit(tblDept dept)
{
using (DBModel db = new DBModel())
{
if (dept.DeptID == 0)
{
db.tblDepts.Add(dept);
db.SaveChanges();
return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
else {
db.Entry(dept).State = EntityState.Modified;
db.SaveChanges();
return Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
[HttpPost]
public ActionResult Delete(int id)
{
using (DBModel db = new DBModel())
{
tblDept dept = db.tblDepts.Where(x => x.DeptID == id).FirstOrDefault<tblDept>();
db.tblDepts.Remove(dept);
db.SaveChanges();
return Json(new { success = true, message = "Deleted 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 |
|---|
