Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
会话变量的jQuery MVC 5下拉列表_Jquery_Asp.net Mvc - Fatal编程技术网

会话变量的jQuery MVC 5下拉列表

会话变量的jQuery MVC 5下拉列表,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我正在尝试用下拉列表的值设置会话变量,如果它是值的文本,我将不进行融合 $(document).ready(function () { $("#slid").on("change", function () { var selectedLang = document.getElementById("slid"); var selectedItem = selectedLang.options[selectedLang.selectedIndex].tex

我正在尝试用下拉列表的值设置会话变量,如果它是值的文本,我将不进行融合

$(document).ready(function () {
    $("#slid").on("change", function () {

        var selectedLang = document.getElementById("slid");
        var selectedItem = selectedLang.options[selectedLang.selectedIndex].text;

        var myvar = selectedItem.text;

        '@{Session["temp"] = "' + myvar + '"; }';

        alert('@{Session["temp"] = "' + myvar + '"; }');


        alert(this.value);

    })
我的警报(this.value)有效,但我试图将其设置为会话变量,但什么也得不到

如果我说

    var text = this.textContent;

    alert(text);
这将返回所有,而不仅仅是一个

更新

ok seing as$。post不起作用

我正在尝试使用ajax回发

        $.ajax({
            url: "@Url.Action("UpdateLang")",
            type: "POST",
            data: { model: JSON.stringify(myVar) },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (response) {
                alert("somthing"); // response.responseText
        },
        success: function (response) {
            alert(response);
        }
这也失败了


需要帮忙吗谢谢你不能那样做。您需要将客户端变量发送回服务器。您可以使用来执行此操作:

Javascript:

$(document).ready(function () {
    $("#slid").on("change", function () {
        // This is the jQuery way of finding selected option's text
        var myVar = $(this).find("option:selected").text();
        // Post the value to your server. You should do some error handling here
        $.post("/MyController/SetSession", {
            myVariable: myVar
        });
    });
});
MyController.cs

[HttpPost]
public ActionResult SetSession(string myVariable) {
    // Set to Session here.
    Session["temp"] = myVariable;
    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

注意:会话实际上不是用来存储短期信息的。也许你可以用?

你不能那样做。您需要将客户端变量发送回服务器。您可以使用来执行此操作:

Javascript:

$(document).ready(function () {
    $("#slid").on("change", function () {
        // This is the jQuery way of finding selected option's text
        var myVar = $(this).find("option:selected").text();
        // Post the value to your server. You should do some error handling here
        $.post("/MyController/SetSession", {
            myVariable: myVar
        });
    });
});
MyController.cs

[HttpPost]
public ActionResult SetSession(string myVariable) {
    // Set to Session here.
    Session["temp"] = myVariable;
    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

注意:会话实际上不是用来存储短期信息的。也许你可以用?

你不能那样做。您需要将客户端变量发送回服务器。您可以使用来执行此操作:

Javascript:

$(document).ready(function () {
    $("#slid").on("change", function () {
        // This is the jQuery way of finding selected option's text
        var myVar = $(this).find("option:selected").text();
        // Post the value to your server. You should do some error handling here
        $.post("/MyController/SetSession", {
            myVariable: myVar
        });
    });
});
MyController.cs

[HttpPost]
public ActionResult SetSession(string myVariable) {
    // Set to Session here.
    Session["temp"] = myVariable;
    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

注意:会话实际上不是用来存储短期信息的。也许你可以用?

你不能那样做。您需要将客户端变量发送回服务器。您可以使用来执行此操作:

Javascript:

$(document).ready(function () {
    $("#slid").on("change", function () {
        // This is the jQuery way of finding selected option's text
        var myVar = $(this).find("option:selected").text();
        // Post the value to your server. You should do some error handling here
        $.post("/MyController/SetSession", {
            myVariable: myVar
        });
    });
});
MyController.cs

[HttpPost]
public ActionResult SetSession(string myVariable) {
    // Set to Session here.
    Session["temp"] = myVariable;
    return new HttpStatusCodeResult(HttpStatusCode.OK);
}


注意:会话实际上不是用来存储短期信息的。如果您试图在javascript中设置会话变量,也许可以使用?

,但这不起作用。C#代码只在服务器上运行,而不是在客户端。是的,我知道这一点。如果没有回发,您无法从javascript设置服务器变量。好的,谢谢,回发现在根本不会发生任何事情,如果您试图在javascript中设置会话变量,这将不起作用。C#代码只在服务器上运行,而不是在客户端。是的,我知道这一点。如果没有回发,您无法从javascript设置服务器变量。好的,谢谢,回发现在根本不会发生任何事情,如果您试图在javascript中设置会话变量,这将不起作用。C#代码只在服务器上运行,而不是在客户端。是的,我知道这一点。如果没有回发,您无法从javascript设置服务器变量。好的,谢谢,回发现在根本不会发生任何事情,如果您试图在javascript中设置会话变量,这将不起作用。C#代码只在服务器上运行,而不是在客户端。是的,我知道这一点。你不能在没有回发的情况下从javascript设置服务器变量。好的,谢谢,回发现在不会发生任何事情,我确信这会起作用,但我尝试实现这一点,但我什么也没有得到,所以现在实际不起作用的是回发,我将所选ID设置为myVar变量,但之后它失败了$。post没有做任何事情:如果你知道为什么像你这样的回发在MVC 5中不起作用,你能说明为什么,我会让你的回答正确吗?我确信这会起作用,但我尝试实现,但我什么也没有得到,所以现在实际上不起作用的是回邮,我将所选ID设置为myVar变量,但之后它失败了$。post没有做任何事情:如果你知道为什么像你这样的回发在MVC 5中不起作用,你能说明为什么,我会让你的回答正确吗?我确信这会起作用,但我尝试实现,但我什么也没有得到,所以现在实际上不起作用的是回邮,我将所选ID设置为myVar变量,但之后它失败了$。post没有做任何事情:如果你知道为什么像你这样的回发在MVC 5中不起作用,你能说明为什么,我会让你的回答正确吗?我确信这会起作用,但我尝试实现,但我什么也没有得到,因此,现在实际不起作用的是回发,我将所选ID设置为myVar变量,但在这之后,它失败了$。回发没有任何作用:如果你知道为什么像你这样的回发在MVC 5中不起作用,你能说明原因吗?我会给出正确的答案