0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that fixed it! Why is my not working? Can you explain that?
As I said look on your url link. You try to connect razor and js and write to url string which is @Html.Raw(Url.Action(%22Bestellen%22,%20%22Cart%22,%20new%20%7B%20orderForDate=orderForDate%7D)) and it is not real path to your Controller and Action.

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.