将变量传递给部分视图而不使用mvc中的Session变量

时间:2014-07-30 06:25:27

标签: asp.net-mvc session

基本上我正在处理两种类型的登录。一个是UserLogin,另一个是客户端登录。我想把这两个分开。所以我在登录时传递会话变量,如下所示,

//这是作为客户端登录时的

Session["LoginAs"] = "client";

//这是以用户身份登录

Session["LoginAs"] = "user";

但这里有一个问题。会话在2分钟内超时。如果我在配置文件中增加也只有相同的结果。所以我不想把这两个传递给会话。如果有任何其他选择可用意味着它将是完美的。

1 个答案:

答案 0 :(得分:1)

我猜Sessions因应用程序回收而重置。您应该尝试使用StateServer session mode

但是,使用模型执行此操作的正确方法。我建议你这样做。

        public class LoginModel {
            public bool IsClient { get; set; }
            public bool IsUser { get; set; }
        }

        public ActionResult Index()
        {
            LoginModel model = new LoginModel();
            model.IsClient = true;

            return View(model);
        }

查看

@model LoginModel
@if (Model.IsClient)
{
    //Client Login
}
else
{
    //User Login
}

<强>已更新

另外,为了在其他部分视图中识别登录方法。

    [HttpPost]
    public ActionResult Index(LoginModel model)
    {
        if (model.IsClient)
        {
            //Identify if Client Login and pass to some other partial view wich you need
            OtherPartialViewModel m = new OtherPartialViewModel();
            m.IsClient = true;
        }


        return View(model);
    }