[英]Passing a date between two pages
Ok, so I have an MVC webapp.好的,所以我有一个 MVC webapp。 I've tried for hours to pass one simple variable from TransactionsDatePicker.cshtml to Transactions display.我已经尝试了几个小时将 TransactionsDatePicker.cshtml 中的一个简单变量传递给 Transactions 显示。
I have an input with an id of 'transactionlookupdate'.我有一个 ID 为“transactionlookupdate”的输入。 I want to intercept it (input type is date).我想拦截它(输入类型是日期)。
I've managed to append the date to the link like this:我已经设法将日期链接到 append,如下所示:
<script>
document.getElementById("buttoncontinue").addEventListener("click", function () {
dateSelected = document.getElementById("transactionlookupdate").value;
document.location.href = 'TransactionsDisplay' + '/' + dateSelected;
});
</script>
Now, what do I do in TransactionsDisplay (where I want to get the date) to store it in usable variable?!现在,我在 TransactionsDisplay(我想获取日期的地方)中做什么以将其存储在可用变量中?!
So far I've tried like a 100 different ways, one that got me the closest was:到目前为止,我已经尝试了 100 种不同的方法,其中最接近我的方法是:
(top of TransactionsDisplay.cshtml) (TransactionsDisplay.cshtml 的顶部)
@{
ViewBag.Title = "TransactionsDisplay";
Layout = "~/Views/Shared/_Layout.cshtml";
var dateSelected = Request.Url.Segments.Last();
}
and awful try at populating alert with dateSelected:并糟糕地尝试使用 dateSelected 填充警报:
<script>
function myFunction() {
alert(dateSelected);
}
</script>
Any help would be appreciated!任何帮助,将不胜感激!
Pass the date as url parameter in TransactionsDatePicker.cshtm在TransactionsDatePicker.cshtm中将日期作为 url 参数传递
document.location.href = 'TransactionsDisplay' + '?date=' + dateSelected;
and extract in TransactionsDisplay at the end of the <body> element:并在 <body> 元素末尾的TransactionsDisplay中提取:
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const myDate = urlParams.get('date');
alert(myDate); // test alert
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.