验证错误未显示在网格中

发布于 2024-12-11 04:03:51 字数 4267 浏览 0 评论 0原文

我有一个 MVC 3 Razor Telerik 网格。单击“编辑更新”按钮时,将执行正确的控制器并执行 TryUpdateModel 语句。

我故意输入了一些我知道会导致错误的文本。 TryUpdateModel 返回 false(在这种情况下是预期的),然后执行 return 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();
        }

I have an MVC 3 Razor Telerik grid. When the Update for an Edit button is clicked, the proper Controller is executed and the TryUpdateModel statement is performed.

I purposely put in some text that I know whould casuse an error. The TryUpdateModel returns false (which is expected in this circumstance) and then executes the return View(); statement.

However, I get back a modal dialog box that says "The requested URL returned a 500 error". If I click the modal dialog and look on the grid, no validation msg appears.

If I am not using a Telerik grid with just input boxes, the validation msg appears fine from my DataAnnotations that reside inside my Model.

What am I doing incorrectly?

Here is my View:

@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()
 )

Here is my Controller:

[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();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文