English 中文(简体)
ModelBinder 不会更新会话键。
原标题:ModelBinder does not update the session key

I created a Website(it is poco object) model binder, that checks the sessions: public class WebsitesModelBinder:IModelBinder { private const string websitesSessionName = "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
}

In the global.asax file I registered the model binder for typeof website. In my controller action, the action gets the website as a parameters and updates it such as: public ActionResult Websites(Website SelectedSite) {
var sites = db.Websites.ToList(); if (SelectedSite.ID == 0) SelectedSite = sites[0]; ViewData["Selectedsite"] = SelectedSite;

        return View(sites);
    }

However model binder never updates the session Any ideas?

问题回答

我知道这已经过时了,可能不再有用,但我在搜索时遇到了这个问题,想从我所知道的回答。如果删除抛出异常的if子句,那么这应该可以工作。您正在返回一个引用,因此应该适当进行更新。希望这至少有助于未来。





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签