【发布时间】: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