Search code examples
javascriptactionresult

C# MVC load ActionResult with javascript with parameter


I want to load an action result with javascript. I found some solution on stackoverflow, but they aint working.

I have this piece of code that when its friday it should load this view. It looks like this:

 var from = jQuery('#orderForDate').datepicker().val().split("-");
        var f = new Date(from[2], from[1] - 1, from[0]);
        var n = f.getDay();

        orderForDate = jQuery('#orderForDate').datepicker({ dateFormat: "dd-mm-yy" }).val();

        if (n == 5) {
            console.log('friday baby!');
            var url = '@Html.Raw(Url.Action("Bestellen", "Cart", new { orderForDate=orderForDate}))';
            window.location = url;
        }

This is the controller is should load:

 public ActionResult Bestellen(string orderForDate)
    {
        ViewBag.date = string.IsNullOrEmpty(orderForDate) ? DateTime.Now.Date : DateTime.ParseExact(orderForDate, "dd-MM-yyyy", CultureInfo.GetCultureInfo("nl-NL"));
        User user = _db.Users.Find(_db.GetCurrentUserId());

        var vm = new BestellenViewModel { ShowFavoritesAsDefault = user.ShowFavoritesAsDefault };
        return PartialView(vm);
    }

The problem is, when I click on a date that is friday in my datepicker, the browsers loads this url/page

http://localhost:54408/Cart/@Html.Raw(Url.Action(%22Bestellen%22,%20%22Cart%22,%20new%20%7B%20orderForDate=orderForDate%7D))

And I obviously don't get the desired page.

What am I missing here?


Solution

  • Read your url link. http://localhost:54408/Cart/@Html.Raw(Url.Action(%22Bestellen%22,%20%22Cart%22,%20new%20%7B%20orderForDate=orderForDate%7D)). It is not what you want. Try: window.location = '/Cart/Bestellen?orderForDate=' + orderForDate