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

I get this error when I try to update: "The key-value pairs that define an EntityKey cannot be null or empty"

3 Answers 269 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dylan
Top achievements
Rank 1
Dylan asked on 31 May 2013, 05:55 PM
I've been following the steps outlined here to practice CRUD operations with the Kendo UI Grid:

http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/batch-editing

And the incell editing is not actually saving the data changes I make. I've figured out that I'm running into ModelState errors, and so the ModelState.IsValid flag is false. Here is my edit method:

public ActionResult Editing_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<TestTable> tests)
        {
            var results = new List<TestTable>();
 
            foreach (var modelStateValue in ViewData.ModelState.Values)
            {
                foreach (var error in modelStateValue.Errors)
                {
                    // Do something useful with these properties
                    var errorMessage = error.ErrorMessage;
                    var exception = error.Exception;
                }
            }
 
            if (tests != null && ModelState.IsValid)
            {
                using (var delta = new DeltaHREntities())
                {
                    foreach (var test in tests)
                    {
                        var entity = new TestTable
                        {
                            ID = test.ID,
                            Name = test.Name,
                            DateCreated = test.DateCreated
                        };
 
                        results.Add(entity);//store the test object for later use
                        delta.TestTables.Attach(entity);
                        //delta.Entry(test).State = EntityState.Modified;//why doesn't this work?
                        delta.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
                    }
                    delta.SaveChanges();
                }
            }
 
            return Json(results.ToDataSourceResult(request, ModelState, test => new TestTable
            {
                Name = test.Name,
                DateCreated = test.DateCreated,
                ID = test.ID
            }));
        }
The exception that is raised is this:

"The key-value pairs that define an EntityKey cannot be null or empty"

Now the only part of my dataset that is null is the Name column, but this is set to Nullable in the edmx file, and the db field itself is set to nullable. So why do I keep getting this error message?

Another thing I'm confused about is how the demo (see the link above ) says to create a class called ProductViewModel in step 5. Why is this necessary when the Entity Framework has already created a Product table for you automatically? This seems to be an unneeded duplication of data. Thanks.

3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 03 Jun 2013, 10:08 AM
Hello Dylan,

 The ProductViewModel is created to avoid the model state validation error caused by the EntityKey property binding. In general using view models is considered a good practice.

 If you don't want to create a view model you should clear the model state errors.

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Freddie
Top achievements
Rank 1
answered on 27 Aug 2013, 06:19 AM
Hello Dylan,

We have a similar problem re ajax binding, did you get the solution?, if yes kindly share it to me because i did everything but until now I can't still find the right solution.

thanks

freddie
0
Daniel
Telerik team
answered on 29 Aug 2013, 04:28 AM
Hello Freddie,

You could use one of the following approaches to avoid the modelstate error:

  1. Create a ViewModel as Atanas already mentioned.
  2. Project the data to an anonymous object with the same properties as demonstrated here.
  3. Exclude the EntityKey from the binding:
    public ActionResult Update([Bind(Prefix="models",Exclude = "EntityKey")]IEnumerable<MyModel> data)
Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Dylan
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Freddie
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or