问题
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; }
}
回答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.
来源:https://stackoverflow.com/questions/30068155/kendo-grid-populates-and-posts-all-of-the-navigation-properties