Please read here and here to get a quick overview of my problem and to see exactly what I mean when I say Model Binding from Ajax.
Would it be a bad idea to make the foreign key fields nullable in order to allow for Model Binding from javascript?
For example, we want to bind to a Person object during an ajax call to... (The Person class is created from the Entity Framework)
public ActionResult Create(Person personToCreate)
{
//Create person here
}
If the Person
had a Pet
object that was a foreign key in the db, you can't pass the Pet
object along with the other data from the ajax call. So unless the Pet
is nullable in the DB, binding to Person
wouldn't work.
So what I want to know is... in order to do Model Binding, should/can I null the db fields I can't pass from javascript? Or do I have to make a Custom Model Binder and bind to a "Flatter" version of the object in order to follow best practices? example of flatter version of the object:
public class SimplePerson() {
private string firstName;
private string lastName;
private string petName;
}
The reason I ask this is because a lot of my Entity Framework created classes contain foreign keys, which means I'll need to create a flat duplicate of nearly all of those classes, and it seems to go against the whole DRY principal.