将数据保存到数据库后,我试图从控制器发送成功或失败消息。 我知道这个问题可能会重复,但是我尝试了Internet上的每个选项,但对我来说都不起作用。 当我尝试这些答案时,有时没有错误出来,但输出也没有出来。.有时,当调试时,我发现“ 500(内部服务器错误)”。
我的代码在这里: 我的保存功能脚本:
function Save() {
var txtVal = $("#txtDescription").val();
if (txtVal == "") {
alert("Please Enter Description!");
}
else {
var url = "/Master/SavetoDB";
$.ajax({
url: url,
data: {txtVal},
cache: false,
type: "GET",
});
$('input[type="text"]').val("");
}
}
我的控制器:
public ActionResult Category()
{
return View();
}
public ActionResult SavetoDB(string txtVal)
{
DateTime tranDate = DateTime.Now;
using (var db = new SWDataContext())
{
//try
//{
IEnumerable<Category> _PCList = db.Category.AsNoTracking();
CategoryPRPC = db.Category.Where(a => a.Description.ToUpper() == txtVal.ToUpper()).FirstOrDefault();
if (!string.IsNullOrEmpty(txtVal))
{
if (PRPC != null)
{
if (PRPC.Description.ToUpper() == txtVal.ToUpper())
{
//return Content("-----");
ViewBag.Scripts = "<script>alert('Duplicate Record- Record already Exists!!!')</script>";
//TempData["AlertMessage"] = "Duplicate Record- Record already Exists!!!";
//ViewBag.Message = string.Format("Duplicate Record- Record already Exists!!!");
//ViewData["Success"] = "Duplicate Record- Record already Exists!!!";
//return new JavascriptResult { Script = "alert('Duplicate Record- Record already Exists!!!');" };
//return Content("<script language='javascript' type='text/javascript'>alert('Category saved Successfully!!!');</script>");
//throw new Exception("Duplicate Record- Record already Exists!!!");
//return Content("<script language='javascript' type='text/javascript'>alert('Duplicate Record- Record already Exists!!!');</script>");
}
//throw new Exception("Duplicate Record- Record already Exists!!!");
//return JavaScript(alert("Duplicate Record- Record already Exists!!!"));
}
else if (PRPC == null)
{
Category new_PRPC = new Category
{
Description = txtVal,
CreatedDate = tranDate,
CreatedUser="Admin",
};
db.Category.Add(new_PRPC);
db.SaveChanges();
ViewBag.Scripts = "<script>alert('hello')</script>";
//TempData["AlertMessage"] = "Category saved Successfully!!!";
//ViewData["Success"] = "Category saved Successfully!!!'";
//return Content("<script language='javascript' type='text/javascript'>alert('Category saved Successfully!!!');</script>");
}
}
//}
//catch (Exception ex)
//{
// return Json(new { success = "Fail", message = ex.Message }, JsonRequestBehavior.AllowGet);
//}
//return View("Category");
}
return View(ViewBag); /*Content("Data added successfully");*/
//return View("Category");
//return Json(new { result = result });
//return new JavascriptResult { Script = "alert('Category saved Successfully!!!'');" };
//return Content("<script language='javascript' type='text/javascript'>alert('Category saved Successfully!!!');</script>");
//return Json(new { success = "success", message = "Data saved successfully" }, JsonRequestBehavior.AllowGet);
}
我输入了所有从互联网答案中尝试过的注释代码。
看来,我什至尝试过
@if (ViewBag.SuccessMessage != null)
{
<text>
<script>
$(document).ready(function () {
alert('@ViewBag.SuccessMessage');
});
</script>
</text>
}
使用此命令时,出现500内部服务器错误。如何解决此问题。请帮助。