Razor/MVC OnPostUpdate Model Class Instance always empty

3 posts, 0 answers
  1. Chris
    Chris avatar
    2 posts
    Member since:
    Aug 2020

    Posted 05 Nov 2020 Link to this post

    I have a fairly simple Kendo Grid in a Razor page that posts to its Model via Ajax

    All the parameter on the grid are set correctly (Forgery Token, etc). Also set the right camel case in startup.cs:

                    .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
                    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); 

    The post successfully hits the OnPostUpdate function but the class only has empty members:

    If I access the Form via the Request object (not the "request" object), I can see all members correctly posted but the "doesnotwork" instance only contains empty fields as if the deserialization did not work correctly.

            public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, hrFeedback doesnotwork)
            {
                hrFeedback tFeedback=null;
                try
                {
                    var t=CU.CGuid(Request.Form["FeedID"]);
                    tFeedback = _context.tblFeedback.Where(s => s.FeedID == t).FirstOrDefault();
                    if (tFeedback != null)
                    {
                        tFeedback.Feedback = Request.Form["Feedback"];// feedback.Feedback;
                        tFeedback.FollowedUp = Request.Form["FollowedUp"]; // feedback.FollowedUp;
                        tFeedback.Subject = Request.Form["Subject"]; // feedback.Subject;
                        tFeedback.Unsubscribe = CU.CDate(Request.Form["Unsubscribe"]).DateTime; // feedback.Unsubscribe;
                        _context.tblFeedback.Update(tFeedback);
                        _context.SaveChanges();
                    }
                    else
                        return new JsonResult(Content("Feedback ID not found")); //Is there a better way to tell the grid that the update failed?
                }
                catch (Exception e)
                {

                }
                return new JsonResult(new[] { tFeedback }.ToDataSourceResult(request, ModelState));
            }

     

    I also assume that my OnPostRead does not work right as I can see the filter/sort coming in via Request.Form["filter"] but the ToDataSourceResult seems not to take these values as the result on the page never shows filter or search results (always all rows are displayed)

            public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request)
            {
                var t = new JsonResult(_context.tblFeedback.ToDataSourceResult(request));
                return t;
            }

     

    Can anybody tell me what I am forgetting?

    I have another page that works just fine using the OnPostUpdate and its class value but I cannot see any difference between the two

     

    Thanks

  2. Chris
    Chris avatar
    2 posts
    Member since:
    Aug 2020

    Posted 06 Nov 2020 in reply to Chris Link to this post

    Update: Today I came into my office and magically it suddenly works. I have the feeling the "Entity Framework" is caching wrong information. 

    How can I make sure the Entity Framework is up-to-date?

    Thanks

  3. Nikolay
    Admin
    Nikolay  avatar
    301 posts

    Posted 10 Nov 2020 Link to this post

    Hi Chris,

    Thank you for the update o nthe case.

    This seems to be a problem with the local environmental setup rather than a Kendo UI related one. I can suggest you to refer to the Microsoft documentation page for "Entity Framework" inquiries.

    Additionally, below I am posting a link to a Kendo UI article that provides solutions for common issues you might encounter while working with the Telerik UI Grid HtmlHelper for ASP.NET Core.

    Let me know if you have any questions.

    Regards,
    Nikolay
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top