如何在其他剃须刀页面中访问一个剃刀页面的JS变量?

时间:2017-12-08 11:59:29

标签: javascript c# asp.net-mvc razor

我如何访问变量" DataToBind" Two.cshtml页面中的One.cshtml页面?

One.cshtml

<script>
var DataToBind;

    $.ajax({
            url: "/Home/getDetails",
            type: "POST",
            dataType: "json",
            data: JSON.stringify({ agencyID: agencyID, salesID: saleID, DebtorID: debtorID, AccountNo: accountNO }),
            contentType: 'application/json; charset=utf-8',
            success: function (response) {
                DataToBind = response[0];

                window.location.href = "Two";
            }, error: function () {

                console.log("new error");
            }
        });
</script>

Two.cshtml

我收到此错误:未捕获的ReferenceError:未定义DataToBind 当我使用&#34; DataToBind&#34;此页面中的变量。

1 个答案:

答案 0 :(得分:1)

据我了解,您正试图获得一些价值并在另一页中使用它。 这有点不寻常,但你最好的拍摄是

<script>
var DataToBind;

$.ajax({
        url: "/Home/getDetails",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ agencyID: agencyID, salesID: saleID, DebtorID: debtorID, AccountNo: accountNO }),
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            c= response[0];

            window.location.href = "Two?data=" + DataToBind;
        }, error: function () {

            console.log("new error");
        }
    });

</script>

并在您的第二页中访问此数据

<script>
        var DataToBind =  @(Request.QueryString["data"])
        ...
     </script>

或其他方式

<script>
var DataToBind = @HttpContext.Current.Request.QueryString["data"]
...
</script>

然后你可以在Two.cshtml上使用你的DataToBind