我已经看过一个关于使用jquery模式弹出窗口进行基本crud操作的教程,试过它并且它工作但是当我尝试将其与其他数据一起使用时,我得到的唯一问题是当我点击提交按钮时没有任何反应,没有错误也没有消息,它已成功保存到数据库。请帮忙
我正在使用Asp.net MVC C#
AccountsController:
public ActionResult Save(int id = 0)
{
List<Branch> Branch = GetBranch();
if (id > 0)
{
var i = GetInfo(id);
if (i != null)
{
ViewBag.Branches = new SelectList(Branch, "BranchID", "BranchName", i.BranchID);
}
else
{
return HttpNotFound();
}
return PartialView("Save", i);
}
else
{
ViewBag.Branches = new SelectList(Branch, "BranchID", "BranchName");
return PartialView("Save");
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(AccountManager am)
{
string message = "";
bool status = false;
if (ModelState.IsValid)
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
if (am.ManagerID > 0)
{
var v = dc.AccountManagers.Where(a => a.ManagerID.Equals(am.ManagerID)).FirstOrDefault();
if (v != null)
{
v.ManagerName = am.ManagerName;
v.ContactNo = am.ContactNo;
v.BranchID = am.BranchID;
v.DateCreated = am.DateCreated;
}
else
{
return HttpNotFound();
}
}
else
{
dc.AccountManagers.Add(am);
}
dc.SaveChanges();
status = true;
message = "Successfully Saved.";
}
}
else
{
message = "Error! Please try again.";
}
return new JsonResult { Data = new { status = status, message = message } };
}
JavaScript的:
var $dialog; var $rowCount;
$(document).ready(function () {
//Save Contacts
$("body").on('submit', '#saveForm', function (e) {
e.preventDefault();
SaveContacts();
});
});
//Save Contact
function SaveContacts() {
//Validation
if ($('#ManagerName').val().trim() == '' ||
$('#ContactNo').val().trim() == '' ||
$('#BranchID').val().trim() == '' ||
$('#DateCreated').val().trim() == '') {
$('#msg').html('<div class="failed">All fields are required.</div>');
return false;
}
var contact = {
ManagerID: $('#ManagerID').val() == '' ? '0' : $('#ManagerID').val(),
ManagerName: $('#ManagerName').val().trim(),
ContactNo: $('#ContactNo').val().trim(),
BranchID: $('#BranchID').val().trim(),
DateCreated: $('#DateCreated').val().trim()
};
//Add validation token
contact.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
//Save Contact
$.ajax({
url: '/accounts/Save',
type: 'POST',
data: contact,
dataType: 'json',
success: function (data) {
alert(data.message);
if (data.status) {
$('#ManagerID').val('');
$('#ManagerName').val('');
$('#ContactNo').val('');
$('#BranchID').val('');
$('#DateCreated').val('');
LoadContacts();
$dialog.dialog('close');
}
},
error: function () {
$('#msg').html('<div class="failed">Error! Please try again.</div>');
}
});
}