scottdavidwalker
scottdavidwalker

Reputation: 1541

No data appearing from web api call to angular front when returning a view model using Odata

Returning a View model to my from my web api to my angular front end and I'm not receiving any data. The response is: @odata.context: {"http://localhost:4141/odata/$metadata#Inspection/$entity", id: 0}

but it should also be returning a list of inspectors and inspections. The object creates on the Api side correctly but doesn't transfer into my front end?

Angular code:

  function getInspections() {
            inspectionService.getByCompanyId(vm.company.id).then(
                function (response) {
                    console.log(response)
                    vm.inspection = response.value;
                })
            };

API code

 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");
    }

ViewModel:

public class InspectionDetailViewModel
{ 

    [Key]
    public int Id { get; set; }
    public List<InspectionDto> Inspections { get; set; }
    public List<Inspector> Inspectors { get; set; }

}

Odata Builder:

builder.EntitySet<InspectionDetailViewModel>("Inspection");
        builder
            .EntityType<InspectionDetailViewModel>()
            .Collection
            .Function("GetByCompanyId")
            .ReturnsCollectionFromEntitySet<InspectionDetailViewModel>("Inspection")
            .Parameter<string>("Id");

So it returns the Id but no other data, even though the data is there on the web api at the time of return.

Any ideas?

Upvotes: 0

Views: 200

Answers (1)

Fan Ouyang
Fan Ouyang

Reputation: 2132

It seems that Inspections and Inspectors are built as Entity type, there is a Id or Key in the model? you need to use $expand to get the navigation property expand like:

?$expand=Inspections,Inspectors

If you want them to by complex type, you need explicitly call like:

builder.ComplexType<Inspector>();

Upvotes: 0

Related Questions