This is the query string that I am receiving in URL.
Output url: /demo/analysis/test?startDate=Sat+Jun+01+2013+00%3A00%3A00+GMT-0700+(Pacific+Daylight+Time)&endDate=Fri+Sep+13+2013+00%3A00%3A00+GMT-0700+(Pacific+Daylight+Time)
test controller:
Public Function GetData(<FromUri()> ByVal query As Request) As HttpResponseMessage
{
}
Request{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
Angularjs Api代码:
exportFunt = function () {
var query = generateExportData();
Export(query);
};
function generateExportData() {
return {
startDate: viewModel.StartDate,
endDate: viewModel.EndDate,
};
}
Export: function (query) {
$window.open('/demo/analysis/test?$.param(query), '_blank');
}
答案 0 :(得分:2)
由于您不需要时间,我会假设您从某种日期选择器中选择整个日历日期。只需将日期作为字符串以yyyy-mm-dd
格式传递:
function pad(n) {
return n < 10 ? '0' + n : n;
}
function getDateString(dt) {
return [dt.getFullYear(),pad(dt.getMonth()+1),pad(dt.getDate())].join('-');
}
function generateExportData() {
return {
startDate: getDateString(viewModel.StartDate),
endDate: getDateString(viewModel.EndDate)
};
}
或者,如果您不想自己编写所有这些内容,可以考虑使用优秀的moment.js库:
function generateExportData() {
return {
startDate: moment(viewModel.StartDate).format('YYYY-MM-DD'),
endDate: moment(viewModel.EndDate).format('YYYY-MM-DD')
};
}
答案 1 :(得分:0)
我修改了发送startDate&amp;的Javascript。 endDate采用“mm / dd / yyyy”格式。 这有效,因为asp.net控制器方法能够在datetime类型中获取此日期..
exportFunt = function () {
var query = generateExportData();
query .startDate = [query.startDate.getMonth() + 1,
query.startDate.getDate(),
query.startDate.getFullYear()].join('/');
query .endDate = [query.endDate.getMonth() + 1,
query.endDate.getDate(),
query.endDate.getFullYear()].join('/');
Export(query);
};