0

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?

1 Answer 1

0

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>();
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.