views:

135

answers:

1

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.

+1  A: 

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.

Graham
Ya, I agree about the database not seeming like the right solution, that's the reason I'm asking. Allowing stuff to be null that shouldn't be is just asking for problems, I want to be covered for that... because if myself or anyone else were to work against the database in the future, they may make mistakes and the DB wouldn't end up preventing it.On the other hand, the amount of code to cover all the classes seems extremely extensive. I was hoping for maybe an inbetween or for someone to give me the "OK" to null those DB variables.
Matt