我已尝试过在网上和网上其他地方找到的几个解决方案,但我仍然无法让服务器发送错误以正确显示在jqgrid编辑表单上。我已经在jqgrid的afterSubmit方法中添加了断点但它从未被命中。我已将其简化为只显示警报,如果方法已执行但没有任何工作。我最终得到的是堆栈跟踪显示在编辑表单的标题中。我错过了什么?
JQGrid代码
[ResponseType(typeof(HttpResponseMessage))]
[HttpPut]
public HttpResponseMessage Put(int id, [FromBody] InvoiceTypeModel item)
{
if (item == null)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read Invoice Type from body");
InvoiceType originalItem = db.InvoiceTypes.FirstOrDefault(x => x.Id == id);
if (originalItem == null || originalItem.Id != id)
{
item.Id = 0;
Post(item);
}
InvoiceType nameMatchItem = db.InvoiceTypes.FirstOrDefault(x => x.Name == item.Name);
if (nameMatchItem != null && nameMatchItem.Id != item.Id)
return Request.CreateErrorResponse(HttpStatusCode.Conflict, "You can't have more than one record with the same name");
if (ModelState.IsValid && id == item.Id)
{
try
{
item.Id = id;
ObjectHelper.CopyPropertyValues(item, originalItem);
db.InvoiceTypes.ApplyCurrentValues(originalItem);
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound, "Record not found");
}
catch (Exception ex)
{
string c = ex.Message;
}
return Request.CreateResponse(HttpStatusCode.OK, "Update Successful");
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database.");
}
}
Web API控制器方法代码
[HttpPost]
public ActionResult EditData(string oper, int? Id, string Name, DateTime? DateCreated, int? Active)
{
InvoiceTypeModel cn = new InvoiceTypeModel();
switch (oper)
{
case "add":
{
cn.Id = 0;
cn.Name = Name;
cn.DateCreated = DateTime.Now;
cn.UpdatedBy = User.Identity.Name;
cn.Active = true;
_invoiceTypeRepository.Create(cn);
return Content("true");
}
case "del":
{
_invoiceTypeRepository.Delete((int) Id);
return Content("true");
}
case "edit":
{
cn.Id = (int) Id;
cn.Name = Name;
cn.DateCreated = (DateTime) DateCreated;
cn.DateUpdated = DateTime.Now;
cn.UpdatedBy = User.Identity.Name;
cn.Active = Active == 1;
_invoiceTypeRepository.Upsert(cn);
return Content("true");
}
}
return Content("false");
}
这是网页控制器代码
int n = int.Parse(textBox7.Text); ;
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
numbers[i] = int.Parse(textBox1.Text);
}
// var array = new short[] { 4, 4, 5, 6,9 };
var sum = numbers.Select(x => (int)x).Sum();
var avg = numbers.Select(x => (int)x).Average();
var max = numbers.Select(x => (int)x).Max();
var min = numbers.Select(x => (int)x).Min();
textBox4.Text = sum.ToString();
textBox5.Text = avg.ToString();
textBox2.Text = min.ToString();
textBox3.Text = max.ToString();
这是编辑表单收到错误后的部分屏幕截图。这是正确的信息,409冲突但格式化没有正确发生。