Angularjs 如何使用Angular将表单输入中的值发布到WebApi
您需要将HTML表单包装在代码中,如下所示Angularjs 如何使用Angular将表单输入中的值发布到WebApi,angularjs,asp.net-web-api,Angularjs,Asp.net Web Api,您需要将HTML表单包装在代码中,如下所示 public string Post([FromBody]dynamic value) { return some value from form here; // JToken } <form novalidate name="frm1"> <div class="input-group col-md-12"> <input type="text" class="form-control
public string Post([FromBody]dynamic value)
{
return some value from form here; // JToken
}
<form novalidate name="frm1">
<div class="input-group col-md-12">
<input type="text" class="form-control input-lg" name="firstName" ng-model="user.firstName" />
<input type="text" class="form-control input-lg" name="lastName" ng-model="user.lastName" />
<span class="input-group-btn">
<button class="btn btn-info btn-lg" type="button" ng-click="submit($event, user)">
<i class="glyphicon glyphicon-search"></i>
</button>
</span>
</div>
</form>
这里我给出了从表users获取用户列表的代码示例
步骤1:
public JsonResult UserLogin()
{
使用(codesnip Db=new codesnip())
{
var user=Db.users.ToList();
返回Json(用户,JsonRequestBehavior.AllowGet);
}
}
第2步:进入角度js
步骤2.1创建角度角度角度应用程序
var SampleApp=angular.module('SampleApp',[]);
步骤2.2为视图创建控制器
SampleApp.controller('HomeCtrl',['$scope',函数($scope){
}]);
步骤2.3创建获取用户数据的服务
工厂('userData',函数($http){
var fac={};
fac.GetUsers=函数(){
返回$http.get('/Home/UserLogin');//Controllername/action
}
返回fac;
});
创建服务后,在控制器中使用此服务,如下所示
步骤2.4
在html中
GetAllUsers
控制器('HomeCtrl',['$scope','userData',函数($scope,userData){
$scope.usersList=null;
$scope.getUserdata=function(){
userData.GetUsers().then(函数(d){
$scope.usersList=d.data;
})
}
}]);
您查过$http.post了吗?看见使用ng model
和ng submit
指令。
<form novalidate name="frm1">
<div class="input-group col-md-12">
<input type="text" class="form-control input-lg" name="firstName" ng-model="user.firstName" />
<input type="text" class="form-control input-lg" name="lastName" ng-model="user.lastName" />
<span class="input-group-btn">
<button class="btn btn-info btn-lg" type="button" ng-click="submit($event, user)">
<i class="glyphicon glyphicon-search"></i>
</button>
</span>
</div>
</form>
angular.module('main').controller('someController', function($scope, $http) {
$scope.user = {
firstName: null,
lastName: null
};
$scope.submit = function($event, user) {
$http.post('api/users', user)
.then(function(response) {
console.log('success');
}).catch(error) {
console.log(error);
}
};
});
here am giving the Code example for getting the users list from the table Users
Step 1:
public JsonResult UserLogin()
{
using (Codesnnip Db = new Codesnnip())
{
var user = Db.users.ToList();
return Json(user, JsonRequestBehavior.AllowGet);
}
}
Step 2: step in to angular js
Step 2.1 create a angular anglar app
var SampleApp = angular.module('SampleApp',[]);
Step 2.2 create controller for the View
SampleApp.controller('HomeCtrl',['$scope',function($scope){
}]);
step 2.3 create a service to getting the users data
SampleApp.factory('userData',function($http){
var fac = {};
fac.GetUsers = function(){
return $http.get('/Home/UserLogin'); // Controllername/action
}
return fac;
});
after creating the service use this service in the controller like this
step 2.4
in html
<button ng-click="getUserdata()">GetAllUsers</button>
SampleApp.controller('HomeCtrl',['$scope','userData',function($scope,userData){
$scope.usersList = null;
$scope.getUserdata = function(){
userData.GetUsers().then(function(d){
$scope.usersList = d.data;
})
}
}]);