更新后刷新会话

时间:2017-12-03 06:29:00

标签: asp.net-mvc session-variables session-state

我希望会话在更新后获得新值,但在我重新登录之前总会得到旧值...

我为VIP会员提供了一个名为VIP的小屏幕截图: https://i.stack.imgur.com/DVhAZ.png

当我点击有效的Vip时,值会发生变化,但在视图中没有图像。

我的观点:

@if (session.IsVip == false)
{
    <p>Xin Chào @session.UserName!</p>
    <span>@Html.ActionLink("Đăng xuất", "Logout", "User")</span>
}
else
{
    <p>Xin Chào @session.UserName!</p> <img src="~/Assets/Client/img/Vip.png" />
    <span>@Html.ActionLink("Đăng xuất", "Logout", "User")</span>
}

我的控制器:

public ActionResult Load(string id,khachhang c)
{
    var session = Session[CommonConstants.USER_SESSION] as UserLogin;
    var mm = new MuaVipModel();
    mm.setupvip(session.UserID);
    return View();
}

查看:

@using (Html.BeginForm("Load","MuaVip",FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Thanh toán thành công, bạn có thể kích hoạt VIP!</h4>
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Kích hoạt vip" class="btn btn-default" />
            </div>
        </div>
    </div>
}

我的存储过程:

create PROCEDURE [dbo].[setupVip] 
    @MaKhachHang nvarchar(10)
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE khachhang 
    SET IsVip = 1 
    WHERE MaKhachHang = @MaKhachHang
END

登录:

var dao = new UserDao();
var result = dao.Login(model.Username, Encryptor.MD5Hash(model.Password), false);
if (result == 1)
{
    var user = dao.GetById(model.Username);
    var userSession = new UserLogin();
    userSession.UserName = user.Username;
    userSession.UserID = user.MaKhachHang;
    userSession.GroupID = user.MaNhomNguoiDung;
    userSession.IsVip = (bool)user.IsVip;
    var listCredentials = dao.GetListCredential(model.Username);
    Session.Add(CommonConstants.SESSION_CREDENTIALS, listCredentials);
    Session.Add(CommonConstants.USER_SESSION, userSession);
    return RedirectToAction("Index", "Home");
}

1 个答案:

答案 0 :(得分:0)

如果您想随时添加或更新会话值,那么您可以这样写:

Session["UserName"]="XYZ";  // Session UserName will have value XYZ

如果你想更新这个,你的另一种方法。

Session["UserName"]="ABC"; // Session UserName new value ABC