Hi, I've run into a little issue with my custom model binder and nhibernate. It is obviously an issue of not really understanding how to use nhibernate properly. Here is my scenario. Let's take the following example:
class Foo{ int id; IList<string> list; }
class Unit{ int id; string name; }
class Product{ Foo _foo; Unit _unit; DateTime _date; }
I have a form to create a new Product that displays two drop down lists containing existing Foo and Unit objects and a text box for the _date. My custom model binder looks something like this:
class ProductModelBinder : IModelBinder{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){ int unitId; int fooId; Product p = new Product(); // Get UnitId and FooId selected by the user via the drop down list.. p.Unit = UnitRepository.GetById(unitId); p.Foo = FooRepository.GetById(fooId); } }
My model binder works fine, unless there is some kind of error or validation issue that occurs in my Action handler or something. If I have any kind of issues following the model binder, the Product object gets persisted to the database when it should not! Why is this exactly? I know it has something to do with the fact that I'm assigning p.Unit and p.Foo to items that currently exist in the database, but I'm not really sure how to get around this issue. What am I fundamentally doing wrong here? I know I'm missing something.
Thanks! Bryan
|