Question

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.

Was it helpful?

Solution

I read over what you linked and posted, I can't really think of a good solution off the top of my head, but the whole concept of changing your underlying database for the sake of AJAX makes me uncomfortable. I know that's not a great answer, I'm struggling with several EF design issues right now myself, and there have been several times when I've been tempted to modify the database for the sake of the model, but doing that has always come back to bite me in the past.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top