【问题标题】:Session variable defaults to the last one set会话变量默认为最后一组
【发布时间】:2019-12-11 15:33:25
【问题描述】:

我知道我这样做是不好的做法,但我仍然想了解为什么这不起作用。

我有 3 个onclick 用于 Javascript 中不同网格的方法:

    $('body').on('click', '#clinician-appointments>tbody>tr>td:not(:last-child):not(:first-child)', function () {
         var id = $(this).closest("tr").children("td.grid__col--id").find("[name=patient-link]").text().trim();
         @{Session["Clinician"] = "opa"};
         location.href = "@Url.Action("Summary", "Patient")" + "/" + id;
             });

    $('body').on('click', '#clinician-current-admissions>tbody>tr>td:not(:last-child):not(:first-child)', function () {
        var id = $(this).closest("tr").children("td.grid__col--id").find("[name=patient-link]").text().trim();
        location.href = "@Url.Action("Summary", "Patient")" + "/" + id;
       @{Session["Clinician"] = "ca"};
            });

    $('body').on('click', '#clinician-diagnostics>tbody>tr>td:not(:last-child):not(:first-child)', function () {
        var id = $(this).closest("tr").children("td.grid__col--id").find("[name=patient-link]").text().trim();
        location.href = "@Url.Action("Summary", "Patient")" + "/" + id;
        @{Session["Clinician"] = "diag"};
            });  

但是,当我在 C# 中调用会话时,它总是“诊断”,即最后一个分配的。

因此,由于他们参加不同的赛事,其他赛事将被错过。

【问题讨论】:

    标签: javascript c# jquery razor


    【解决方案1】:

    由于这是一个 Razor 页面,当你这样做时:

    @{Session["Clinician"] = "diag"};
    

    这将在页面在浏览器中呈现之前执行。所以随着页面的加载,下面会依次执行server-side:

    @{Session["Clinician"] = "opa"};
    @{Session["Clinician"] = "ca"};
    @{Session["Clinician"] = "diag"};
    

    与您的预期相反,它们不会在单击相应的网格时被执行,因为那是一个纯粹的客户端交互事件。

    我建议你替换 "@{Session["Clinician"] = "..."};"使用 ajax 调用新的控制器操作,在会话中设置字符串。所以是这样的:

    JavaScript:

    function Session()
    {
    }
    
    Session.SetString = function (key, value)
    {
        $.post("/Home/SessionString?key=" + key + "&value=" + value);
    };
    
    Session.GetString = function (key, successCallback)
    {
        $.get("/Home/SessionString?key=" + key, null, function (data, textStatus, jqXHR) { successCallback(data) });
    };
    
    $('body').on('click', '#clinician-appointments>tbody>tr>td:not(:last-child):not(:first-child)', function () {
         var id = $(this).closest("tr").children("td.grid__col--id").find("[name=patient-link]").text().trim();
         Session.SetString("Clinician", "opa");
         location.href = "@Url.Action("Summary", "Patient")" + "/" + id;
    });
    

    控制器(用于 .NET Core):

        /// <summary>
        /// Sets a value in the user's session.
        /// </summary>
        /// <param name="key">The key for the value.</param>
        /// <param name="value">The value.</param>
        [HttpPost]
        public IActionResult SessionString(string key, string value)
        {
            HttpContext.Session.SetString(key, value);
            return new JsonResult(null);
        }
    
        /// <summary>
        /// Get a value from the user's session.
        /// </summary>
        /// <param name="key">The key for the value.</param>
        /// <returns>The value.</returns>
        [HttpGet]
        public IActionResult SessionString(string key)
        {
            string value = HttpContext.Session.GetString(key);
            return new JsonResult(value);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多