【问题标题】:Nhibernate, MVC and ModelBindersNhibernate、MVC 和 ModelBinders
【发布时间】:2008-12-30 13:16:00
【问题描述】:

我想用 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。

任何想法我做错了什么?

【问题讨论】:

    标签: asp.net-mvc nhibernate model-view-controller modelbinders


    【解决方案1】:

    在第一个示例中,您通过 Spring.NET 检索您的对象。这意味着它将查找所有依赖项并将它们粘贴到您的对象中并且一切正常。

    在第二个示例中,您一直都忘记了 Spring.NET,而只是创建了一个普通的类实例。

    您注册活页夹的行应如下所示:

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

    其中 context 是 Spring.NET 包中的 IApplicationContext 或 IObjectFactory 实例。

    最好的问候, 马蒂亚斯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多