【问题标题】:Passing a date between two pages在两页之间传递日期
【发布时间】:2021-02-17 17:03:34
【问题描述】:

好的,所以我有一个 MVC webapp。我已经尝试了几个小时将一个简单的变量从 TransactionsDatePicker.cshtml 传递到 Transactions display。

我有一个 id 为“transactionlookupdate”的输入。我要截取(输入类型为日期)。

我已经设法将日期附加到链接中,如下所示:

<script>
document.getElementById("buttoncontinue").addEventListener("click", function () {
    dateSelected = document.getElementById("transactionlookupdate").value;
    document.location.href = 'TransactionsDisplay' + '/' + dateSelected;
});
</script>

现在,我在 TransactionsDisplay(我想要获取日期的位置)中做什么以将其存储在可用变量中?!

到目前为止,我已经尝试了 100 种不同的方法,其中最接近我的是:

(TransactionsDisplay.cshtml 的顶部)

@{
    ViewBag.Title = "TransactionsDisplay";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var dateSelected = Request.Url.Segments.Last();
}

并且糟糕的尝试使用 dateSelected 填充警报:

<script>
    function myFunction() {
        alert(dateSelected);
    }
</script>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript html model-view-controller


    【解决方案1】:

    TransactionsDatePicker.cshtm

    中将 date 作为 url 参数传递
    document.location.href = 'TransactionsDisplay' + '?date=' + dateSelected;
    

    并在

    元素末尾的TransactionsDisplay中提取:
    <script>
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const myDate = urlParams.get('date');
    
    alert(myDate); // test alert
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      相关资源
      最近更新 更多