Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用asp.net core mvc通过angular js获取和发布数据_C#_Angularjs_Asp.net Core - Fatal编程技术网

C# 无法使用asp.net core mvc通过angular js获取和发布数据

C# 无法使用asp.net core mvc通过angular js获取和发布数据,c#,angularjs,asp.net-core,C#,Angularjs,Asp.net Core,我想在一个视图中使用AngularJs功能,在这个视图中我想获取记录并添加记录 我正在使用下面的代码,但没有得到预期的结果 <div ng-app="MyApp" ng-controller="MyController"> <table id="tblCustomers" class="table" cellpadding="0" cellspacing=

我想在一个视图中使用AngularJs功能,在这个视图中我想获取记录并添加记录 我正在使用下面的代码,但没有得到预期的结果

    <div ng-app="MyApp" ng-controller="MyController">
        <table id="tblCustomers" class="table" cellpadding="0" cellspacing="0">
            <tr>
                <th style="width:100px">Customer Id</th>
                <th style="width:150px">Name</th>
                <th style="width:150px">Country</th>
                <th style="width:100px"></th>
            </tr>
            <tbody ng-repeat="m in Customers">
                <tr>
                    <td><span>{{m.CustomerId}}</span></td>
                    <td>
                        <span ng-hide="m.EditMode">{{m.Name}}</span>
                        <input type="text" ng-model="m.Name" ng-show="m.EditMode" />
                    </td>
                    <td>
                        <span ng-hide="m.EditMode">{{m.Country}}</span>
                        <input type="text" ng-model="m.Country" ng-show="m.EditMode" />
                    </td>
                    <td>
                        @*<a class="Edit" href="javascript:;" ng-hide="m.EditMode" ng-click="Edit($index)">Edit</a>
                        <a class="Update" href="javascript:;" ng-show="m.EditMode" ng-click="Update($index)">Update</a>
                        <a class="Cancel" href="javascript:;" ng-show="m.EditMode" ng-click="Cancel($index)">Cancel</a>
                        <a href="javascript:;" ng-hide="m.EditMode" ng-click="Delete(m.CustomerId)">Delete</a>*@
                    </td>
                </tr>
            </tbody>
        </table>
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td style="width: 150px">
                    Name<br />
                    <input type="text" ng-model="Name" style="width:140px" />
                </td>
                <td style="width: 150px">
                    Country:<br />
                    <input type="text" ng-model="Country" style="width:140px" />
                </td>
                <td style="width: 200px">
                    <br />
                    <input type="button" value="Add" ng-click="Add()" />
                </td>
            </tr>
        </table>
    </div>
    
    
    
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope, $http, $window) {
            //Getting records from database.
            var post = $http({
                method: "GET",
                url: "/Customer/GetCustomers",
                dataType: 'json',
                headers: { "Content-Type": "application/json" }
            });
            post.success(function (data, status) {
                //The received response is saved in Customers array.
                $scope.Customers = data;
            });
    
            //Adding new record to database.
            $scope.Add = function () {
                if (typeof ($scope.Name) == "undefined" || typeof ($scope.Country) == "undefined") {
                    return;
                }
                var post = $http({
                    method: "POST",
                    url: "/Customer/InsertCustomer",
                    data: "{name: '" + $scope.Name + "', country: '" + $scope.Country + "'}",,
                    dataType: 'json',
                    headers: { "Content-Type": "application/json" }
                });
                post.success(function (data, status) {
                    //The newly inserted record is inserted into the Customers array.
                    $scope.Customers.push(data)
                });
                $scope.Name = "";
                $scope.Country = "";
            };
        });
    </script>

客户Id
名称
国家
{{m.CustomerId}
{{m.Name}}
{{m.Country}
@*
*@
名称
国家:

var app=angular.module('MyApp',[]) app.controller('MyController',函数($scope,$http,$window){ //从数据库中获取记录。 var post=$http({ 方法:“获取”, url:“/Customer/GetCustomers”, 数据类型:“json”, 标题:{“内容类型”:“应用程序/json”} }); 发布成功(功能(数据、状态){ //收到的响应保存在客户阵列中。 $scope.Customers=数据; }); //向数据库添加新记录。 $scope.Add=函数(){ if(typeof($scope.Name)=“未定义”| | typeof($scope.Country)=“未定义”){ 返回; } var post=$http({ 方法:“张贴”, url:“/Customer/InsertCustomer”, 数据:“{name:'”+$scope.name+”,国家:“+$scope.country+”,”,, 数据类型:“json”, 标题:{“内容类型”:“应用程序/json”} }); 发布成功(功能(数据、状态){ //新插入的记录将插入到Customers数组中。 $scope.Customers.push(数据) }); $scope.Name=“”; $scope.Country=“”; }; });
这是我的控制器代码,在第一种方法中,我在调试模式下获取所有记录,我在获取所有客户,在保存方法中,我没有获取插入的文本数据

    public JsonResult GetCustomers()
    {       
        var Customers = db.Query<Customer>("Select * from Customer");
        return Json(Customers);
    }

    [HttpPost]
    public JsonResult InsertCustomer(string name, string country)
    {
        Customer customer = new Customer();
        customer.Name = name;
        customer.Country = country;
        db.Save(customer);
        return Json(customer);
    }
publicjsonresult GetCustomers()
{       
var Customers=db.Query(“从客户中选择*);
返回Json(客户);
}
[HttpPost]
public JsonResult InsertCustomer(字符串名称、字符串国家/地区)
{
客户=新客户();
customer.Name=名称;
customer.Country=Country;
db.Save(客户);
返回Json(客户);
}
我既没有收到任何记录,也无法插入任何记录。有人能帮我解决问题吗

更新 没有记录可以很好地显示数据

m.CustomerId
更改为
m.CustomerId

m.Name
更改为
m.Name

m.Country
更改为
m.Country

     ...
     <tbody>
        <tr ng-repeat="m in Customers">
            <td><span>{{m.customerid}}</span></td>
            <td>
                <span ng-hide="m.EditMode">{{m.name}}</span>
                <input type="text" ng-model="m.name" ng-show="m.EditMode" />
            </td>
            <td>
                <span ng-hide="m.EditMode">{{m.country}}</span>
                <input type="text" ng-model="m.country" ng-show="m.EditMode" />
            </td>
            <td>
                @*<a class="Edit" href="javascript:;" ng-hide="m.EditMode" ng-click="Edit($index)">Edit</a>
                    <a class="Update" href="javascript:;" ng-show="m.EditMode" ng-click="Update($index)">Update</a>
                    <a class="Cancel" href="javascript:;" ng-show="m.EditMode" ng-click="Cancel($index)">Cancel</a>
                    <a href="javascript:;" ng-hide="m.EditMode" ng-click="Delete(m.CustomerId)">Delete</a>*@
            </td>
        </tr>
    </tbody>
     ...
测试截图







测试后,主要问题是angular的
$http
无法正确传递值,您需要将
数据更改为
参数

测试截图



但我无法获取记录,而且我只获取了没有数据的空行。是否导入了您的Js并成功发送了请求?我在浏览器工具中获取了正确的记录,但鉴于此,它将表中的空间累积为黑行没有显示文本没有我没有导入此内容
            var post = $http({
                method: "POST",
                url: "/Customer/InsertCustomer",
                data: $.param({ name: $scope.Name, country: $scope.Country }),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }

            });