【发布时间】:2016-08-25 14:03:59
【问题描述】:
从我的 web api 到我的 angular 前端将 View 模型返回给我,但我没有收到任何数据。 响应为:@odata.context: {"http://localhost:4141/odata/$metadata#Inspection/$entity", id: 0}
但它也应该返回检查员和检查的列表。对象在 Api 端正确创建但没有传输到我的前端?
角度代码:
function getInspections() {
inspectionService.getByCompanyId(vm.company.id).then(
function (response) {
console.log(response)
vm.inspection = response.value;
})
};
API 代码
public async Task<InspectionDetailViewModel> GetByCompanyId([FromODataUri] string id)
{
Guid companyId;
var valid = Guid.TryParse(id, out companyId);
if (valid)
{
var inspection = GetAllInspections(companyId)
var inspectors = await GetAllInspectors();
var ins = new InspectionDetailViewModel()
{
Inspections = inspection,
Inspectors = inspectors
};
return ins;
}
// return BadRequest("Invalid Company Id");
}
视图模型:
public class InspectionDetailViewModel
{
[Key]
public int Id { get; set; }
public List<InspectionDto> Inspections { get; set; }
public List<Inspector> Inspectors { get; set; }
}
Odata 生成器:
builder.EntitySet<InspectionDetailViewModel>("Inspection");
builder
.EntityType<InspectionDetailViewModel>()
.Collection
.Function("GetByCompanyId")
.ReturnsCollectionFromEntitySet<InspectionDetailViewModel>("Inspection")
.Parameter<string>("Id");
所以它返回的是 Id 但没有其他数据,即使返回时数据在 web api 上。
有什么想法吗?
【问题讨论】:
标签: angularjs asp.net-web-api2 odata asp.net-mvc-viewmodel