【问题标题】:No data appearing from web api call to angular front when returning a view model using Odata使用 Odata 返回视图模型时,没有从 Web api 调用到 Angular Front 出现数据
【发布时间】: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


    【解决方案1】:

    Inspections 和 Inspectors 好像是构建为 Entity 类型的,模型中有 Id 或 Key 吗?你需要使用$expand 来让导航属性展开如下:

    ?$expand=Inspections,Inspectors
    

    如果您希望它们按复杂类型,则需要显式调用:

    builder.ComplexType<Inspector>();
    

    【讨论】:

    猜你喜欢
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2017-12-23
    • 2020-11-11
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多