【问题标题】:ModelBinder does not update the session keyModelBinder 不更新会话密钥
【发布时间】:2010-02-16 21:32:28
【问题描述】:

我创建了一个网站(它是 poco 对象)模型绑定器,用于检查会话: 公共类 WebsitesModelBinder:IModelBinder { private const string websiteSessionName = "SelectedSite";

    #region IModelBinder Members

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.Model != null)
            throw new InvalidOperationException("Invalid");

        Website site = (Website)controllerContext.HttpContext.Session[websitesSessionName];
        if (site == null)
        {
            site = new Website();
            controllerContext.HttpContext.Session[websitesSessionName] = site;
        }
        return site;
    }

    #endregion
}

在 global.asax 文件中,我为 typeof 网站注册了模型绑定器。 在我的控制器操作中,该操作将网站作为参数获取并对其进行更新,例如: 公共 ActionResult 网站(Website SelectedSite) {
var 站点 = db.Websites.ToList(); if (SelectedSite.ID == 0) SelectedSite = 站点[0]; ViewData["Selectedsite"] = SelectedSite;

        return View(sites);
    }

但是模型绑定器从不更新会话 有什么想法吗?

【问题讨论】:

  • 您的格式略有偏差,使代码更难阅读。

标签: model-view-controller session object modelbinder


【解决方案1】:

我知道这是旧的,可​​能不再有用,但我在搜索自己的过程中遇到了这个问题,并想从我所做的事情中回答。如果您删除引发异常的 if 子句,那么这应该可以工作。您正在返回一个参考,所以这应该适当地更新。希望这至少对未来有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2016-12-05
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多