我有一个MenuRole页面,显示数据库中的菜单列表。用户必须从页面中选择一个角色和相应的菜单。一切顺利但是当我试图保存数据时,需要很长时间来保存数据(约50秒)。有些时间timeout exception
即将到来。要插入的最大记录数为25
。
保存多条记录的方法有什么缺点吗?
如何在更短的时间内保存数据?
按钮点击事件
$("#btnSubmit").click(function () {
var form = $(this).closest("form");
if (form.valid()) {
isLoading(true);
var formData = $('#frmRoles').serialize();
$.ajax({
type: "POST",
url: "@Url.Action("Add","MenuSettings")",
data: formData,
datatype: "json",
success: function (data) {
isLoading(false);
if (data.message == "success") {
clearCheckBox();
toastr.success("Successfully saved the details.")
}
else if (data.message == "warning") {
toastr.info("Cannot save the data.RoleName already exists")
}
else {
toastr.error("Validaiton error.")
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
isLoading(false);
toastr.error("Uncaught error.");
}
});
}
return false;
});
控制器
[HttpPost]
public JsonResult Add(MenuRoleVM _vmMenuRole)
{
try
{
if (ModelState.IsValid)
{
//Check whether role already exists in RoleMenu table
var _roleCount = _db.MenuRoles
.Where(m => m.RoleID == _vmMenuRole.RoleId).Distinct().ToList();
if (_roleCount.Count > 0)
{
return Json(new { message = "warning" }, JsonRequestBehavior.AllowGet);
}
else
{
for (int i = 0; i < _vmMenuRole.MenuList.Count; i++)
{
MenuRole _menuRole = new MenuRole();
_menuRole.MenuID = _vmMenuRole.MenuList[i].MenuId;
_menuRole.RoleID = _vmMenuRole.RoleId;
_menuRole.CanAdd = _vmMenuRole.MenuList[i].CanAdd;
_menuRole.CanEdit = _vmMenuRole.MenuList[i].CanEdit;
_menuRole.CanDelete = _vmMenuRole.MenuList[i].CanDelete;
_menuRole.CanView = _vmMenuRole.MenuList[i].CanView;
_menuRole.TransactionTime = DateTime.Now;
_db.MenuRoles.Add(_menuRole);
}
_db.SaveChanges();
return Json(new { message = "success" }, JsonRequestBehavior.AllowGet);
}
}
return Json(new { message = "validationError" }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { message = "Error: Cannot Insert" + ex.Message }, JsonRequestBehavior.AllowGet);
}