0

I wish to send a string to my home controller with ASP.NET MVC. I'm doing this using an Ajax call, however I need to change the view too while sending the string.

The problem is that in order to change page, we need to do it in the Ajax success handler. This results in us calling the controller method once again. This time the date string is null, though, resulting in a null exception. How can we work around this?

our AJAX call:

dayClick: function(date) {

            $.ajax({
                url: '/Home/Booking',
                data: {'selectedDate' : date.format()},
                type: "get",
                cache: false,
                success: function (result) {
                    window.location.href = "/Home/Booking"
                },
                error: function (error) {
                    console.log(error.message);
                }
            });

        }

our controller method:

        public IActionResult Booking(string selectedDate)
    {
        var booking = new Booking();

        DateTime selectedDatetime = DateTime.ParseExact(selectedDate, "yyyy-MM-dd", CultureInfo.CurrentCulture);
        booking.Date = selectedDatetime;

        var viewModel = new BookingsideViewModel
        {
            Subjects = new[] {"Matematik", "Dansk", "Engelsk"},
            Booking = booking
        };


        return View(viewModel);
    }
4
  • 2
    Making an ajax call when you want to redirect is just crazy (and all you are doing is calling exactly the same method twice, and ignoring the first call which returns the html back to the browser which you never eve use (just how bad do you want the performance of your app to be?) Commented May 8, 2018 at 12:14
  • Yea it seems kind of silly. How do you suggest we go about it? Commented May 9, 2018 at 9:06
  • 2
    Just make a normal submit (using a form with FormMethod.Get). Of if you like writing extra scripts, then it would be just `dayClick: function(date) { window.location.href = "/Home/Booking?selectedDate=" + date.format(); } Commented May 9, 2018 at 9:17
  • Thanks. I changed it to 'dayClick: function(date) { window.location.href = "/Home/Booking?selectedDate=" + date.format();'. Seems much more more logical now Commented May 9, 2018 at 9:29

2 Answers 2

3

When calling controller method again in success handler we again need to pass date with url

dayClick: function(date) {

        $.ajax({
            url: '/Home/Booking',
            data: {'selectedDate' : date.format()},
            type: "get",
            cache: false,
            success: function (result) {
                window.location.href = "/Home/Booking?selectedDate=" +  date.format();
            },
            error: function (error) {
                console.log(error.message);
            }
        });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

this is silly and inefficient, it just ends making two HTTP requests (one ajax, one normal) where one would have been sufficient. The AJAX call is totally redundant, it doesn't do anything useful. Only the "normal" request (made via window.location.href) is needed. See Stephen's comment on the question, and Hasan's answer which makes more sense.
2

you should send it in URL as query parameter like below:

window.location.href = "/Home/Booking?selectedDate=" + date.format()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.