0

I have a Kendo UI Grid which uses the Serial class as its model. This class has relation with the row class, but I don't use any of the navigation properties in my Grid and I don't need them in this particular page.

The problem is, Kendo UI populates all of the foreign key relations. So the row class and all of the navigation properties of itself will be populated. When I try to save my edit, Kendo posts all of these data and this causes ModelState.IsValid always be false. Do you have any suggestion?

This is the Serial class, and I have a field for each property.

public class Serial
{
    [Key]
    [Column(TypeName = "BIGINT", Order = 0)]
    public Int64 LiIdR { get; set; }
    [ForeignKey("LiIdR")]
    public virtual Rows Row { get; set; }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int16 SRadifS { get; set; }
    public string AFromSerial { get; set; }
    public string AToSerial { get; set; }
    public int? IQnty { get; set; }
    public string AExpireDate { get; set; }
    public string AComment { get; set; }
}
Nic
  • 12,220
  • 20
  • 77
  • 105
Akbari
  • 2,369
  • 7
  • 45
  • 85
  • Did you fix your issue in another way? – Nic May 13 '15 at 06:01
  • No, unfortunately I didn't find any other way. My classes have a lot of fields and it's really painful to create view models for all of them. – Akbari May 13 '15 at 06:06
  • 1
    Can always generate them using https://visualstudiogallery.msdn.microsoft.com/655aa6d4-4461-42ea-aeec-64cdb1313de7 ;) – Nic May 13 '15 at 06:14

1 Answers1

1

It's generally a bad practice to use the generated Entity Framework objects in your view. Your view should not be dependent on the data access layer.

What you should do is to convert the EF entities into view models (containing only what your grid needs) in your business/data layer. This will solve your issue.

On edit/create you just convert the view model back into a EF object and save your changes to the database.

If, for some reason, your view needs certain properties which you don't want to show to the user, you can add them to the grid as Hidden() columns. Then they'll get posted back to the controller.

Nic
  • 12,220
  • 20
  • 77
  • 105