【问题标题】:New to Angular. Trying to pass data back to server using angular / webapiAngular 新手。尝试使用 angular / webapi 将数据传回服务器
【发布时间】:2017-07-04 09:20:17
【问题描述】:

我正在尝试使用 webapi 以角度将数据传递给服务器。感谢论坛上的人,我能够基于另一个使用实体框架和角度来填充 1 个下拉列表。接下来我想弄清楚的是如何使用 webapi 将屏幕数据传回服务器。

调用 Angular 代码时出现错误:badreq 'Http request configuration must be an object'

此错误显示在 $http

我在网上找到了一个示例,它显示了创建每个 html 控件使用的父模型的能力。对于在线示例,他们使用的是文本框,但我使用的是选择列表,但我认为这也应该有效。

谁能告诉我我做错了什么?

我很感激!!

跟进:此时我遇到的问题似乎是 Angular saveattributecontroller 中的数据状态为未定义。所以我现在不相信的问题是对 webapi 的调用,但数据不是从 HTML 中的“详细信息”传递的。

FOLLOW UP 2:基于下面 Lorenzo 的评论。通过将attributevaluecontroller 放在提交按钮周围,我现在可以在attribute.js 中看到传递给saveattributecontroller 的数据,这很好。我还意识到我需要在 saveattributecontroller 中引用 Data.A 和 Data.V。但现在似乎对 WebAPIAttribute 控制器的调用没有发生。我尝试了我最初的方式和之前建议的其他方式,但对控制器的调用似乎从未通过。谁能帮我解决这个问题?

Follow UP 3:我在单步执行 angular javascript 时发现的错误是 Resource can't be found。所以我假设由于某种原因它没有找到 webapi 控制器。这可能是非常简单的事情,但我没有看到它。

var myapp = angular.module('attributeapp', []);
 
    myapp.controller('attributecontroller', function ($scope, $http) {
        $http.get('/Attribute/AttributeIndex/').then(function (response) {
            $scope.Attributes = response.data;
        })
    })
 
 
    myapp.controller('attributevaluecontroller', function ($scope, $http) {
        $scope.getattributevalues = function (id)
        {
            $http.get('/Attribute/getattributevalues/' + id).then(function (response) {
                $scope.A = id;
                $scope.AttributeValues = response.data;
            })
        }
    })
 
 
    myapp.controller('saveattributecontroller', function($scope, $http){
        $scope.attributesave = function (Data) {
            var GetAll = new Object();
            GetAll.AttributeKey = Data.AttributeKey;
            GetAll.AttributeValueKey = Data.AttributeValueKey;
            $http({
                url: "WebAPIAttribute/attributesave",
                dataType: 'json',
                method: 'POST',
                data: GetAll,
                headers: {
                    "Content-Type": "application/json"
                }
            }).success(function (response) {
                $scope.value = response;
            })
               .error(function (error) {
                   alert(error);
               });
        }
    })
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/attribute.js"></script>
</head>
<body data-ng-app="attributeapp">
    <div data-ng-controller="attributecontroller">
         <span data-ng-controller="attributevaluecontroller">
         <select data-ng-model="detail.A" data-ng-change="getattributevalues(detail.A)" data-ng-options="Attribute.Attribute_Key as Attribute.Attribute_Desc for Attribute in Attributes"><option value="">--Select--</option></select><br />{{detail.A}}
         <select data-ng-model="detail.V" data-ng-options="Attribute_Value.Attribute_Value_Key as Attribute_Value.Attribute_Value_Desc for Attribute_Value in AttributeValues"><option value="">--Select--</option></select>{{detail.V}}
         </span>
        <br />
        <span data-ng-controller="saveattributecontroller">
            <input type="button" value="submit" data-ng-click="attributesave(detail)"/>
        </span>
    </div>
</body>
</html>

//AttributeControler.cs
using System;
using System.Collections.Generic;
using MVC_APP1.Models;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
 
namespace MVC_APP1.Controllers
{
    public class AttributeController : Controller
    {
        //
        // GET: /Attribute/
 
        public ActionResult AttributeIndex()
        {
            Cafe_CPDEntities objEntity = new Cafe_CPDEntities();
            var data = objEntity.Attributes.ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }
 
        public ActionResult getattributevalues(int id)
        {
            Cafe_CPDEntities objEntity = new Cafe_CPDEntities();
            var data = objEntity.Attribute_Value.Where(m=>m.Attribute_Key==id);
            //string test = data.FirstOrDefault().Attribute_Value_Desc;
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        public ActionResult attributesave(List<int> ReturnData)
        {
            return null;
        }
 
    }
}

// WebAPIAttributeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
 
namespace MVC_APP1.Controllers
{
    public class WebAPIAttributeController : ApiController
    {
        public class GetAll
        {
            public string AttributeKey { get; set; }
            public string AttributeValueKey { get; set; }
        }
        [HttpPost]
        public string attributesave(HttpRequestMessage request,
            [FromBody] GetAll getAll)
        {
            return "Data Reached";
        }
    }
}
 
 

【问题讨论】:

  • 如果你只做$http.post("WebAPIAttribute/attributesave", GetAll)呢?
  • 能否也添加应用发出的 HTTP 发布请求。
  • 如果在 HTML 中将 attributesave(detail) 更改为 attributesave($scope.detail) 会发生什么? - 在您的操作中数据是否仍然未定义?

标签: asp.net angularjs asp.net-web-api


【解决方案1】:

尝试

    $scope.attributesave = function (Data) {
                var GetAll = new Object();
                GetAll.AttributeKey = Data.AttributeKey;
                GetAll.AttributeValueKey = Data.AttributeValueKey;
                $http.post("WebAPIAttribute/attributesave", getAll)
                   .then(function(response){
                      //here your operations
                   })
                }

【讨论】:

  • 我在上面添加了一个跟进。此时的问题是attribute.js 上的saveattributecontroller 中的数据显示为UNDEFINED。因此,由于某种原因,数据没有从 HTML 传递。不是说你的答案不好,但我只是尝试过,但我仍然遇到同样的问题。非常感谢您的快速响应。你们太棒了!!
  • 我认为问题在于您正在尝试使用保存属性控制器访问同级控制器范围。尝试查看此以进行通信 beetwen 两个兄弟控制器 stackoverflow.com/questions/27709769/…
  • Lorenzo,正如我在上面第二次跟进中提到的那样,这帮助我走得更远,但现在我似乎被困在对 webapi 控制器的调用没有通过。谢谢!
  • 我将新调用添加到似乎有问题的 saveattribute (WEB API) 控制器。
【解决方案2】:

(我以为我之前发布过这个,但它没有保存)

我想出了最后一部分。我需要将 api/ 添加到路由中。

api/WebAPIAttributes/attributesave/

对 webapi 的调用现在通过并传递来自屏幕的数据。

非常感谢所有帮助回答我之前遇到的问题的人。

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多