This is a migrated thread and some comments may be shown as answers.

[Solved] Grid not displaying DataAnnotation msg

4 Answers 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Bill
Top achievements
Rank 2
Bill asked on 19 Oct 2011, 09:12 PM

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

4 Answers, 1 is accepted

Sort by
0
Max
Top achievements
Rank 1
answered on 08 Dec 2011, 09:27 PM
hi there
I am having the same problem. Did you find a solution for this?

Thank you!
0
Bill
Top achievements
Rank 1
answered on 15 Dec 2011, 12:55 AM
This will be the second time I've found the solution myself less than an hour AFTER posting my problems to the board.  But here goes because this will probably affect more than just me.

Symptoms: You have done everything to set validation but only see Server Error 500 messages.  Inspection with Fiddler states there was a Validation Error but you dont know what it is, and it's certainly not the pretty validation message box you expect.

In my case, I had a type mismatch between your ViewModel and my controller because I like the ViewModel approach. I always use ViewModels with my grids to overcome the javascript serialization issue caused by table relationships.  But I never bothered to change my T4(love it) templated Edit method in my controller which was still using the actual entity definition or (POCO).  So while my grid was passing a UserViewModel, the Edit function in my controller was expecting a User.  The edit function will work fine and implicitly convert them because the property names are the same, but these two have to match if you want client side validation to work...at least that was what I had to do after 2 1/2 hours of racking my brain.



0
Bill
Top achievements
Rank 1
answered on 15 Dec 2011, 02:02 AM
Thanks for the post, but I didnt even see it until after I updated my post with my solution.  I think your issue was a bit more complex than mine.  but thanks again.
Tags
Grid
Asked by
Bill
Top achievements
Rank 2
Answers by
Max
Top achievements
Rank 1
Bill
Top achievements
Rank 1
Bill
Top achievements
Rank 2
Share this question
or