首页 > 解决方案 > C# MVC 使用带有参数的 javascript 加载 ActionResult

问题描述

我想用 javascript 加载一个动作结果。我在stackoverflow上找到了一些解决方案,但它们不起作用。

我有这段代码,它应该在星期五加载这个视图。它看起来像这样:

 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;
        }

这是控制器应该加载:

 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);
    }

问题是,当我在日期选择器中单击星期五的日期时,浏览器会加载此 url/页面

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

而且我显然没有得到想要的页面。

我在这里想念什么?

标签: javascriptactionresult

解决方案


阅读您的网址链接。http://localhost:54408/Cart/@Html.Raw(Url.Action(%22Bestellen%22,%20%22Cart%22,%20new%20%7B%20orderForDate=orderForDate%7D)). 这不是你想要的。尝试:window.location = '/Cart/Bestellen?orderForDate=' + orderForDate


推荐阅读