-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Confusion with asp.net mvc custom model binders
PostPosted: Wed Sep 23, 2009 12:11 am 
Newbie

Joined: Tue Sep 01, 2009 5:00 pm
Posts: 3
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.