使用Jquery我试图将日期参数从使用URL.Action
的日期选择器传递到剑道图表。但我无法弄清楚如何将变量添加到网址中,请参见下文。
查看:
var myDate = $(inpDateCompleted).val();
// How to get myDate into the below line to replace 01/11/2013:
var url = '@(Url.Action("HoursByDay", "DashBoard", new { dateCompleted= "01/11/2013" }, null))';
$("#chart").kendoChart({
dataSource: {
transport: {
read: {
url: url,
dataType: "json"
}
},
sort: {
field: "Date",
dir: "asc"
}
},
/// Rest of chart setup removed for clarity
});
控制器:
[HttpGet]
public ActionResult HoursByDay(DateTime dateCompleted)
{
var s = ExecuteSqlCommand2(dateCompleted);
return Content(s, "application/json");
}
答案 0 :(得分:2)
确保您的查询字符串名称与您的操作方法参数名称
匹配var url = '@Url.Action("HoursByDay", "DashBoard",
new { dateCompleted= "01/11/2013" })';
alert(url);
如果要将javascript变量值作为dateCompleted param的值传递,则应首先构建action方法的url(不带任何参数),并在javascript中附加查询字符串值。
var myDate = $(#yourInputFieldIdForDate").val();
alert(myDate);
var url = '@Url.Action("HoursByDay", "DashBoard")?dateCompleted=' + myDate;
alert(url);
替换jQuery选择器以获取符合您要求的日期值。