Nhibernate, MVC and ModelBinders

I want to customize my model bindings using Nhibernate:

So, I have a:

<object id="GigModelBinder" type="App.ModelBinders.GigModelBinder, App.Web"  singleton="false"  >
<property name="VenueManager" ref="VenueManager"/>
<property name="ArtistManager" ref="ArtistManager"/>

      

I have an attribute that marks controller actions so that they use the correct ie model binding

[AcceptVerbs("POST")]
    public ActionResult Create([GigBinderAttribute]Gig gig)
    {
        GigManager.Save(gig);
        return View();
    }

      

This works great and my GigModelBinder has the correct VenueManger and ArtistManager that you injected

However, if in the Start application I add:

System.Web.Mvc.ModelBinders.Binders.Add(typeof(App.Shared.DO.Gig), new GigModelBinder());

      

and in controller action use:

UpdateModel<Gig>(gig);

      

eg:

[AcceptVerbs("POST")]
    public ActionResult Update(Guid id, FormCollection formCollection)
    {
        Gig gig = GigManager.GetByID(id);

        UpdateModel<Gig>(gig);

        GigManager.Save(gig);
        return View();
    }

      

VenueManger and ArtistManager were NOT introduced in GigModelBinder.

Any ideas what I am doing wrong?

+1


source to share


1 answer


In the first example, you will go through Spring.NET to retrieve your object. This means that it will search for all dependencies and inject them into your object and everything works well.

In the second example, you forget about Spring.NET and just create a regular instance of the class.

The line where you register your binder should look like this:




System.Web.Mvc.ModelBinders.Binders[typeof(App.Shared.DO.Gig)] = context.GetObject("GigModelBinder");

      

where context is either IApplicationContext or an IObjectFactory instance from the Spring.NET package.

Regards, Matthias.

+1


source







All Articles