Asp.net 如何将参数传递给AngularJS资源?
我有一个ASP.NET应用程序,表单由AngularJS处理。有两个日期时间选择器(fromDate和toDate变量)和复选框,用于指示是否包括开放日期(包括IsoPenDate) 代码如下所示: 1) 在第一个AngularJS模块中:Asp.net 如何将参数传递给AngularJS资源?,asp.net,angularjs,Asp.net,Angularjs,我有一个ASP.NET应用程序,表单由AngularJS处理。有两个日期时间选择器(fromDate和toDate变量)和复选框,用于指示是否包括开放日期(包括IsoPenDate) 代码如下所示: 1) 在第一个AngularJS模块中: SearchActionsApp.controller("ListController", function($scope, $location,$q, Actions, ActionDates, CounterAgents,Counteragents, A
SearchActionsApp.controller("ListController", function($scope, $location,$q, Actions, ActionDates, CounterAgents,Counteragents, AsyncTask) {
...
$scope.actionsQuery = {
searchText: "",
fromDate: new Date(moment().format("MM/DD/YYYY")),
toDate: new Date(moment().add('d', 3).format("MM/DD/YYYY")),
isOpenDatesIncluded: false,
...
ActionDates.query({ actionId: action.Id }, task.success, task.error);
...
}
2) 在service.js中:
.factory('ActionDates', function ($resource) {
return $resource('/RestApi/api/ActionDates', {});
})
3) 在服务器端:
public IEnumerable<ActionDateItem> QueryActionDates(ActionDateQuery query)
{
var sql = query.BuildSql();
using (var cn = CreateSqlConnection())
{
return cn.Query<ActionDateItem>(sql, new
{
query.ActionId,
query.IsOpenDatesIncluded,
BeginDate = query.FromDate,
EndDate = query.ToDate
});
}
}
public class ActionDateQuery
{
public ActionDateQuery()
{
ActionId = -1;
IsOpenDatesIncluded = false;
FromDate = new DateTime(1980, 1, 1);
ToDate = DateTime.Now.AddYears(1);
}
public long ActionId { get; set; }
public bool IsOpenDatesIncluded { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
}
public IEnumerable QueryActionDates(ActionDateQuery查询)
{
var sql=query.BuildSql();
使用(var cn=CreateSqlConnection())
{
返回cn.Query(sql,新建
{
query.ActionId,
query.isopendate包括在内,
BeginDate=query.FromDate,
EndDate=query.ToDate
});
}
}
公共类ActionDateQuery
{
公共ActionDateQuery()
{
ActionId=-1;
IsOpenDatesIncluded=假;
FromDate=新的日期时间(1980,1,1);
ToDate=DateTime.Now.AddYears(1);
}
公共长ActionId{get;set;}
公共bool isopendates包含{get;set;}
公共日期时间FromDate{get;set;}
公共日期时间ToDate{get;set;}
}
问题是,查询返回的数据集从:1980、1、1到DateTime.Now.AddYears(1),但我需要返回从客户端获得的范围为:fromDate-toDate的数据。除了action.Id之外,我还需要传递3个参数,即fromDate、toDate和isOpenDatesIncluded
如何做到这一点?只需将它们添加到
query
方法的参数中,如下所示:
query({
actionId: action.Id,
fromDate: $scope.actionsQuery.fromDate,
...
}, task.success, task.error)
如果它们没有在资源中预先绑定,它们将作为查询参数附加到请求URL
--编辑--
我还可以建议使用承诺而不是回调,因为这将有助于可读性和链接:
query({
actionId: action.Id,
fromDate: $scope.actionsQuery.fromDate,
...
}).$promise.then(function(response) {
// success
}).catch(function(error) {
// error
});
只需将它们添加到
query
方法的参数中,如下所示:
query({
actionId: action.Id,
fromDate: $scope.actionsQuery.fromDate,
...
}, task.success, task.error)
如果它们没有在资源中预先绑定,它们将作为查询参数附加到请求URL
--编辑--
我还可以建议使用承诺而不是回调,因为这将有助于可读性和链接:
query({
actionId: action.Id,
fromDate: $scope.actionsQuery.fromDate,
...
}).$promise.then(function(response) {
// success
}).catch(function(error) {
// error
});
添加缺少的3个参数,如
{actionId:action.Id、fromDate:fromDate等}
并尝试在资源中绑定参数
$resource('/RestApi/api/ActionDates',{id:'@id',fromdate:'@fromdate',etc})
添加缺少的3个参数,如
{actionId:action.Id、fromDate:fromDate等}
并尝试在资源中绑定参数
$resource('/RestApi/api/ActionDates',{id:'@id',fromdate:'@fromdate',etc}) 出现错误:“ReferenceError:actionsQuery未定义”。您需要使用
$scope
访问它。我发布的代码并不是一个完全相同的复制粘贴使用示例,因为我不知道您的其余代码是如何构造的。如果“From date”的日期控件绑定到actionsQuery.fromDate
,您将使用$scope.actionsQuery.fromDate
访问该变量。确定。该部分现在正在工作,这意味着我要传递3个以上的参数。我想接下来需要更改的是service.js文件中的factory部分,因为我在服务器端没有得到除Id之外的新参数。如何做到这一点?您需要从angular检查请求URL,并验证查询参数是否包含在请求URL中。如果是,您需要验证服务器使用的框架如何处理参数绑定。附加到RestApi URL的参数是正确的,但我无法在服务器端获取这些参数。出现错误:“ReferenceError:actionsQuery未定义”。您需要使用$scope
访问它。我发布的代码并不是一个完全相同的复制粘贴使用示例,因为我不知道您的其余代码是如何构造的。如果“From date”的日期控件绑定到actionsQuery.fromDate
,您将使用$scope.actionsQuery.fromDate
访问该变量。确定。该部分现在正在工作,这意味着我要传递3个以上的参数。我想接下来需要更改的是service.js文件中的factory部分,因为我在服务器端没有得到除Id之外的新参数。如何做到这一点?您需要从angular检查请求URL,并验证查询参数是否包含在请求URL中。如果是,则需要验证服务器使用的框架如何处理参数绑定。附加到RestApi URL的参数是正确的,但我无法在服务器端获取这些参数。我这样做了,但无效:。工厂('ActionDates',函数($resource){return$resource('/RestApi/api/ActionDates'),{actionId:'@actionId',isOpenDatesIncluded:'@isOpenDatesIncluded',fromDate:'@fromDate',toDate:'@toDate'});})您只需要绑定URL字符串中的属性。否则它们将作为查询参数添加。我这样做了,但不起作用:.factory('ActionDates',function($resource){return$resource('/RestApi/api/ActionDates',{actionId:'@actionId',isOpenDatesIncluded:'@isOpenDatesIncluded',fromDate:'@fromDate',toDate:'@toDate'});})您只需要绑定URL字符串中的属性,否则它们将作为查询参数添加。