我正在寻找"添加新客户"在函数" AddOrEdit",当我按下提交时,我无法在浏览器中看到该实体,但数据库已更新。 这个问题发生只是因为我改变了GetData函数,我将FilmName添加到客户表中,从那时起就出现了这个问题。
@{
ViewBag.Title = "Index";
}
<a class="btn btn-primary" style="margin-bottom:10px"
onclick="PopupForm('@Url.Action("AddOrEdit","Customers")')"><i class="fa
fa-plus"></i> New Customer</a>
<h2>Customers</h2>
<div style="width:90%; margin:0 auto;">
<table id="myTable">
<thead>
<tr>
<th>Customer Name</th>
<th>Phone</th>
<th>Film Id</th>
<th>Film Name</th>
<th></th>
</tr>
</thead>
</table>
</div>
<style>
tr.even {
background-color: #F5F5F5;
}
</style>
@* Load datatable css *@
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
<script src="//cdn.datatables.net/1.10.9/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= $('#myTable').DataTable({
"ajax": {
"url": "/Customers/GetData/GetData",
"type": "GET",
"datatype": "json"
},
"columns" : [
{ "data": "CustomerName", "autoWidth": true },
{ "data": "Phone", "autoWidth": true },
{ "data": "FilmId", "autoWidth": true },
{ "data": "FilmName", "autoWidth": true },
{"data":"CustomerId" , "render" : function (data) {
return "<a class='btn btn-default btn-sm'
onclick=PopupForm('@Url.Action("AddOrEdit","Customers")/" +
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: 'Fill Customer Details',
height: 500,
width: 700,
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');
dataTable.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","Customers")/' + id,
success: function (data) {
if (data.success)
{
dataTable.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
})
}
}
});
}
}
</script>
}
这是CustomerController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MoviePro.Models;
using System.Data.Entity;
namespace MoviePro.Controllers
{
public class CustomersController : Controller
{
// GET: Customers
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
MyDatabaseEntities2 db = new MyDatabaseEntities2();
var q = (from c in db.Customers
from Films in c.Films
where Films.FilmId == Films.FilmId
where c.CustomerId == c.CustomerId
select new
{
c.CustomerName,
c.Phone,
c.FilmId,
c.CustomerId,
//FilmName = Films.FilmName
}).ToList();
return Json(new { data = q }, JsonRequestBehavior.AllowGet);
}
public ActionResult loaddata()
{
MyDatabaseEntities2 dc = new MyDatabaseEntities2();
var customers = dc.Customers.Select(c => new
{
c.CustomerName,
c.Phone,
c.FilmId,
c.CustomerId,
});
return Json(new { data = customers }, JsonRequestBehavior.AllowGet);
}
public ActionResult AddOrEdit(int id = 0)
{
if (id == 0)
return View(new Customers());
else
{
using (MyDatabaseEntities2 db = new MyDatabaseEntities2())
{
return View(db.Customers.Where(x => x.CustomerId== id).FirstOrDefault<Customers>());
}
}
}
[HttpPost]
public ActionResult AddOrEdit(Customers customer)
{
using (MyDatabaseEntities2 db = new MyDatabaseEntities2())
{
if (customer.CustomerId == 0)
{
db.Customers.Add(customer);
db.SaveChanges();
return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
else
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
return Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
[HttpPost]
public ActionResult Delete(int id)
{
using (MyDatabaseEntities2 db = new MyDatabaseEntities2())
{
Customers emp = db.Customers.Where(x => x.CustomerId == id).FirstOrDefault<Customers>();
db.Customers.Remove(emp);
db.SaveChanges();
return Json(new { success = true, message = "Deleted Successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
}
答案 0 :(得分:0)
我认为你的ajax请求的网址是错误的。
"ajax": {
"url": "/Customers/GetData/GetData",
"type": "GET",
"datatype": "json"
}
您应该将其更改为
"url": "/Customers/GetData"