简体   繁体   中英

Passing a date between two pages

Ok, so I have an MVC webapp. I've tried for hours to pass one simple variable from TransactionsDatePicker.cshtml to Transactions display.

I have an input with an id of 'transactionlookupdate'. I want to intercept it (input type is date).

I've managed to append the date to the link like this:

<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?!

So far I've tried like a 100 different ways, one that got me the closest was:

(top of TransactionsDisplay.cshtml)

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

and awful try at populating alert with dateSelected:

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

Any help would be appreciated!

Pass the date as url parameter in TransactionsDatePicker.cshtm

document.location.href = 'TransactionsDisplay' + '?date=' + dateSelected;

and extract in TransactionsDisplay at the end of the <body> element:

<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const myDate = urlParams.get('date');

alert(myDate); // test alert
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM