Remove field from create/update

4 posts, 0 answers
  1. Michael
    Michael avatar
    2 posts
    Member since:
    Jun 2015

    Posted 09 Mar Link to this post

    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?  

  2. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    353 posts

    Posted 12 Mar Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Michael
    Michael avatar
    2 posts
    Member since:
    Jun 2015

    Posted 14 Mar in reply to Dimiter Topalov Link to this post

    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?

  5. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    353 posts

    Posted 16 Mar Link to this post

    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!
Back to Top
Kendo UI is VS 2017 Ready