Search code examples
asp.net-mvcsessioncontrollerimodelbinder

MVC 5 Session Variable ModelBinder null on Action Method


I am doing an MVC APP. I have a View that Inherit from Model call UserModel that have 2 properties. UserName and Password. I want to save those values in Session variables, so I am using ModelBinder.

My class definition is like this.

public class UserModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

My model binder is like this.

public class UserDetailModelBinder : IModelBinder
{

    #region Constants

    private const string SessionKey = "User";

    #endregion


    #region Public Methods and Operators

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        UserModel user = (controllerContext.HttpContext.Session != null) ? (controllerContext.HttpContext.Session[SessionKey] as UserModel) : null;

        if (user == null)
        {
            user = new UserDetail();
            controllerContext.HttpContext.Session[SessionKey] = user;
        }

        return user;
    }

    #endregion
}

And I have defined Properly in My global.asax

The problem I found is that my Action Method that receives a UserModel instance from the View is null. It reads what already has my Session instead of Reading the View, and then Save it in the Session.

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(UserModel model)
{
}

I suppose it is because it's the same Model I defined to save in the BinderModel

So, my question would be, How can I save in Session, a Model inherit from the View using BinderModel?


Solution

  • You're setting the null value to UserModel and returned. You should be read the values from request and return it.

    var request = controllerContext.HttpContext.Request;
        if (user == null)
        {
            user = new UserModel() { 
                UserName= request.Form.Get("UserName").ToString(),
    
                Password = request.Form.Get("Password").ToString()
            };
    
            controllerContext.HttpContext.Session["User"] = user;
        }
    

    Instead of using the model binder you could directly storing the user model to session in your login method. I'm not sure why you're choosing model binder.

    public async Task<ActionResult> Login(UserModel model)
    {
        //Session["User"] = model
    }