Javascript 会话变量默认为最后一组
我知道我这样做是不好的,但我想知道为什么这样做不起作用 对于Javascript中的不同网格,我有3种Javascript 会话变量默认为最后一组,javascript,c#,jquery,razor,Javascript,C#,Jquery,Razor,我知道我这样做是不好的,但我想知道为什么这样做不起作用 对于Javascript中的不同网格,我有3种onclick方法: $('body').on('click', '#clinician-appointments>tbody>tr>td:not(:last-child):not(:first-child)', function () { var id = $(this).closest("tr").children("td.grid__col--i
onclick
方法:
$('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#调用会话时,它总是“diag”,即最后一个分配的会话
因此,由于它们处于不同的事件中,其他事件将被忽略。因为这是一个剃刀页面,当您执行以下操作时:
@{Session["Clinician"] = "diag"};
这将在浏览器中呈现页面之前执行。因此,当页面加载时,服务器端会依次执行以下操作:
@{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核心):
//
///设置用户会话中的值。
///
///值的键。
///价值。
[HttpPost]
公共IActionResult会话字符串(字符串键、字符串值)
{
HttpContext.Session.SetString(键,值);
返回新的JsonResult(空);
}
///
///从用户的会话中获取值。
///
///值的键。
///价值。
[HttpGet]
公共IActionResult会话字符串(字符串键)
{
字符串值=HttpContext.Session.GetString(键);
返回新的JsonResult(值);
}
}
/// <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);
}
}