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!

1 thought on “Passing a date between two pages”

  1. 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>
    
    Reply

Leave a Comment