gpt4 book ai didi

angularjs - 将 IEnumerable 从 web api Controller 发送到 angular js

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:28:02 25 4
gpt4 key购买 nike

我正在尝试将 IEnumerable 从 Web API Controller 发送到 AngularJs Controller 。我使用的代码是

网络接口(interface):

readonly InventoryEntities _db = new InventoryEntities();

public IEnumerable<FDVOEligibilityRequest> Get()
{
return _db.FDVOEligibilityRequests.AsEnumerable();
}

AngularJS:

//get all customer information
$http.get("/api/Customer/").success(function (data) {
$scope.requests = data;
$scope.loading = false;
})
.error(function () {
$scope.error = "An Error has occured while loading posts!";
$scope.loading = false;
});

这工作正常,但现在我正在使用 linq 来包含相关表,但它不起作用。 angularJs 代码是一样的。我做错了什么?

readonly InventoryEntities _db = new InventoryEntities();

public IEnumerable<FDVOEligibilityRequest> Get()
{
return _db.FDVOEligibilityRequests
.Include("FDVOEligibilityRequestMandatoryField")
.Include("FDVOEligibilityRequestDCCField").AsEnumerable();
}

我确实在 Controller 中获得了我想要的数据,但是当我尝试将它发送回 Angular 时,我得到了 500(内部服务器错误)angular.js:10722

这就是 FDVOEligibilityRequest 在新的 Web Api Controller 中的样子

public partial class FDVOEligibilityRequest
{
public int Id { get; set; }
public string TestName { get; set; }
public string GroupName { get; set; }
public int MandatoryFieldsID { get; set; }
public int DCCFieldsID { get; set; }
public virtual FDVOEligibilityRequestMandatoryField FDVOEligibilityRequestMandatoryField { get; set; }
public virtual FDVOEligibilityRequestDCCField FDVOEligibilityRequestDCCField { get; set; }
}

最佳答案

如果它是 500 并且发生在您成功地从您的操作返回之后,那么它可能是序列化期间的异常。

我建议检查 FDVOEligibilityRequest 和 FDVOEligibilityRequestMandatoryField/FDVOEligibilityRequestDCCField 之间没有循环引用。

或者尝试使用来自 here 的代码对其进行诊断:

string Serialize<T>(MediaTypeFormatter formatter, T value)
{
// Create a dummy HTTP Content.
Stream stream = new MemoryStream();
var content = new StreamContent(stream);
/// Serialize the object.
formatter.WriteToStreamAsync(typeof(T), value, stream, content, null).Wait();
// Read the serialized string.
stream.Position = 0;
return content.ReadAsStringAsync().Result;
}

try {
var val = _db.FDVOEligibilityRequests
.Include("FDVOEligibilityRequestMandatoryField")
.Include("FDVOEligibilityRequestDCCField").AsEnumerable();
var str = Serialize(new JsonMediaTypeFormatter(), val);
}
catch (Exception x)
{ // break point here
}

ps:common suggestion在这种情况下是使用 DTO 而不是 EF 对象,类似 Automapper .

关于angularjs - 将 IEnumerable 从 web api Controller 发送到 angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com