Nhibernate,MVC和ModelBinders
-
03-07-2019 - |
题
我想用Nhibernate配置我的模型绑定器:
所以我有:
<object id="GigModelBinder" type="App.ModelBinders.GigModelBinder, App.Web" singleton="false" >
<property name="VenueManager" ref="VenueManager"/>
<property name="ArtistManager" ref="ArtistManager"/>
我有一个标记控制器操作的属性,以便他们使用正确的模型绑定器,即
[AcceptVerbs("POST")]
public ActionResult Create([GigBinderAttribute]Gig gig)
{
GigManager.Save(gig);
return View();
}
这很好用,我的GigModelBinder有正确的VenueManger和ArtistManager注入
但是如果在应用程序中开始我添加:
System.Web.Mvc.ModelBinders.Binders.Add(typeof(App.Shared.DO.Gig), new GigModelBinder());
并在控制器操作中使用:
UpdateModel<Gig>(gig);
例如:
[AcceptVerbs("POST")]
public ActionResult Update(Guid id, FormCollection formCollection)
{
Gig gig = GigManager.GetByID(id);
UpdateModel<Gig>(gig);
GigManager.Save(gig);
return View();
}
VenueManger和ArtistManager尚未注入GigModelBinder。
任何想法我做错了什么?
解决方案
在第一个示例中,您将通过Spring.NET检索您的对象。这意味着它将查找所有依赖项并将它们粘贴到您的对象中,并且一切正常。
在第二个例子中,你一直忘记Spring.NET,只是创建一个普通的类实例。
注册活页夹的行应如下所示:
System.Web.Mvc.ModelBinders.Binders[typeof(App.Shared.DO.Gig)] = context.GetObject("GigModelBinder");
其中context是来自Spring.NET包的IApplicationContext或IObjectFactory实例。
祝你好运, 马提亚。
不隶属于 StackOverflow