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

Remove field from create/update

3 Answers 480 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 09 Mar 2016, 10:20 PM

I'm using the Kendo Grid with a Microsoft MVC app, but I don't have the UI for MVC component due to the license we purchased.  We will fix that next year when we renew, but right now I'm just using plain JavaScript.

My datasource is a list of objects from Entity Framework, which includes a Reference field called "PersonType".  When creating new records, I'm having trouble because ModelState.IsValid == false because the reference object IS being created by the MVC binding, but all of it's properties are NULL and some of those values are required.  If the MVC binding didn't create the PersonType object at all there then the ModelState would be valid, because the reference object itself isn't required.  I can see that the grid is passing the reference value as null, but having the property passed caused the binding to create the null object.  I was trying to use the parameterMap to remove these fields, but can't get the syntax correct.

The classes look like this:

  public class PersonType
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Required, StringLength(5)]
        public string PersonTypeId { get; set; }
        [Required, StringLength(100)]
        public string PersonTypeName { get; set; }
    }
 public class Person
    {
        public int PersonId { get; set; }
        public string PersonTypeId { get; set; }
        public PersonType PersonType { get; set; }
    }

It's the PersonType reference field in the Person object that is giving me trouble (PersonTypeId is working correctly).   How can I remove the PersonType reference when the grid posts the create, or what's the best way to handle the reference fields with the Kendo Grid?  

3 Answers, 1 is accepted

Sort by
0
Dimiter Topalov
Telerik team
answered on 12 Mar 2016, 09:04 AM
Hi Michael,

There are several approaches that you can use in the described scenario:

1) Create a PersonType view model, in which the corresponding properties are not required and pass a Person view model with a property of PersonTypeViewModel to the Grid.

2) In the Create action of the controller, manipulate the incoming object, setting its PersonType to null, before saving it to the database.

3) Use the dataSource transport.parameterMap, but please have in mind the that it will not be called if custom functions are used for the CRUD operations. The parameterMap's data.models parameter can be used to manipulate the data, sent to the controller action by the Grid. In this case, the batch option of the dataSource should be set to true.

I hope this helps, but if it doesn't, please provide a runnable example, demonstrating the described issue, so we can investigate it further.

Regards,
Dimiter Topalov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Michael
Top achievements
Rank 1
answered on 14 Mar 2016, 03:54 PM

Thank you for the reply.

I'm curious about how the grid works with the MVC component: do you normally have to remap all Entity Framework objects to viewmodels?  Is there a way to use the grid directly with Entity Framework objects, and if so, how do you get around the errors generated for referenced objects?

0
Dimiter Topalov
Telerik team
answered on 16 Mar 2016, 04:23 PM
Hello Michael,

The Kendo UI Grid can work with any model, it is just considered a good practice to keep the database entities and their view model representations separate.

You can setup the Grid to work directly with the Entity Framework objects. In this case you will have to configure the widget's DataSource properly for the discussed scenario. More specifically, either the default value for the PersonType needs to be specified, or the corresponding field nullable and required properties should be set to true and false, respectively, e.g.:

var dataSource = new kendo.data.DataSource({
schema: {
model: {
id: "PersonId",
fields: {
        ...
PersonType: {
//data type of the field {Number|String|Boolean|Date} default is String
 type: "object",
// used when new model is created, set an object with some default values for the required fields
defaultValue: { ... },
      // you can optionally set the property to be nullable and not required:
      nullable: true,
      required: false
...
        }
      }
    }
  }
});

You can find out more information about the Kendo UI DataSource in the following sections of our documentation and API reference:

http://docs.telerik.com/kendo-ui/framework/datasource/overview

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource

Let me know, if you have further questions, regarding Kendo UI widgets.

Regards,
Dimiter Topalov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Michael
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
Michael
Top achievements
Rank 1
Share this question
or