【问题标题】:MVC 5 Model BindingMVC 5 模型绑定
【发布时间】:2014-10-24 08:00:10
【问题描述】:

我使用 MVC5 和 ASP.NET。我想使用模型绑定。如何在模型绑定中获取会话对象。

   public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {

       //I need session here

    }

【问题讨论】:

  • 您是否正在创建自定义ModelBinder?做什么(我看不出在这种情况下您需要会话对象的原因)?
  • HttpContext.Current.Session?
  • 我找到了我的错。我是从错误的模型活页夹中实现它的。我已经从 System.Web.Mvc.IModelBinder 和 ControllerContext 参数中实现了它。

标签: session asp.net-mvc-5 model-binding


【解决方案1】:
public class SessionPictureModelBinder:System.Web.Mvc.IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var obj = controllerContext.HttpContext.Session["MyKey"];
        return obj;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2016-03-08
    • 2018-06-18
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    相关资源
    最近更新 更多