javascript - 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))
而且我显然没有得到想要的页面。
我在这里想念什么?
解决方案
阅读您的网址链接。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
推荐阅读
- java - 无法最小化 Process.Start 上的新进程窗口
- angular - 如果 Angular 9 中不存在子组件,如何在父元素上设置 display:none?
- vb.net - MovePrevious 从填充的标签中删除文本
- mysql - 我有一个较旧的 .sql 文件(从 5.0.45 导出)我正在尝试通过 phpMyAdmin 导入到较新版本的 MySQL 中。接收错误
- php - 日期显示错误
- arduino - 让两个 arduino 交互而不将它们连接在一起
- typescript - 使用 rxjs 时出错 - 管道和点击运算符
- java - 从 Excel 中读取数值时出现问题
- ios - Xcode 11.4 中 Swift 4 或 5 中的 CircularSpinner 版本
- typescript - 在 Typescript 中声明任何与显式类型哪个具有更好的性能