我有 MVC 3 Razor Telerik网格。单击“更新编辑”按钮时,将执行正确的Controller并执行 TryUpdateModel 语句。
我特意加入了一些我知道会发生错误的文字。 TryUpdateModel返回false(在这种情况下预期),然后执行返回View();言。
但是,我得到一个模态对话框,显示“请求的URL返回500错误”。如果单击模态对话框并查看网格,则不会显示验证消息。
如果我不使用带有输入框的Telerik网格,则验证消息可以从我的模型中的DataAnnotations中显示。
我做错了什么?
这是我的观点:
@model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200);
columns.Bound(o => o.Company).Width(200);
columns.Bound(o => o.FirstName).Width(100).Title("FName");
columns.Bound(o => o.LastName).Width(100).Title("LName");
columns.Bound(o => o.Address1).Width(200).Title("Addr1");
columns.Bound(o => o.Address2).Width(100).Title("Addr2");
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
//columns.Bound(o => o.HomePhone).Width(120);
//columns.Bound(o => o.CellPhone).Width(120);
//columns.Bound(o => o.Website).Width(100);
//columns.Bound(o => o.IMAddress).Width(100);
//columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120);
//columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
)
这是我的控制器:
[HttpPost]
[GridAction]
public ActionResult _SaveAjaxEditing(YeagerTechWcfService.Customer customer)
{
CustomerValidate custValidate = new CustomerValidate();
custValidate.CustomerID = customer.CustomerID;
custValidate.Email = customer.Email;
custValidate.Company = customer.Company;
custValidate.FirstName = customer.FirstName;
custValidate.LastName = customer.LastName;
custValidate.Address1 = customer.Address1;
custValidate.Address2 = customer.Address2;
custValidate.City = customer.City;
custValidate.State = customer.State;
custValidate.Zip = customer.Zip;
custValidate.HomePhone = customer.HomePhone;
custValidate.CellPhone = customer.CellPhone;
custValidate.Website = customer.Website;
custValidate.IMAddress = customer.IMAddress;
if (TryUpdateModel(custValidate))
{
try
{
db.EditCustomer(customer);
TempData["ErrCode"] = "Customer successfully updated.";
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
TempData["ErrCode"] = "CustErr";
ViewBag.Error = ex.Message;
return View();
}
}
else
return View();
}