5

I'm trying to figure out how to properly return a list of custom objects and print its contents using an HTML file.

Here's what the HTTP GET method looks like (for simplicity's sake I've stripped some things down to only illustrate the problem) :

[HttpGet("/atr/testing")]
public List<CustomObject> GetCustomObjects()
{
    //MasterObject.CustomObjects returns a List of CustomObjects
    return MasterObject.CustomObjects();
}

My TypeScript file (called home.ts) subscribes and calls the HttpGet method - here's what the Home class looks like (I've excluded all the imports and @Component section):

export class Home {
public selectedConfiguration: string = 'TESTVALUE';

constructor(private http: Http) {
    this.getConfigurations()
        .subscribe(res => {
            this.selectedConfiguration = res;
        });
}

getConfigurations() {
    console.log('Home:getConfigurations entered...');

    return this.http.get('atr/testing')
        .map((response) => {
            console.log(response);
            return response.json();
        });
}

}

As you can see, the constructor calls 'getConfigurations' which calls the HttpGet method. The part that confuses me is how do I go about using the returned response properly? Form my understanding responses are returned as JSON strings (which is why my selectedConfiguration variable in the Home class is a string value instead of a List). When I try to print out the 'selectedConfiguration' string with {{selectedConfiguration}} in my HTML file I get this:

[object Object],[object Object],[object Object],[object Object]

If I use {{selectedConfiguration[0]}} in my HTML I only get one: [object Object]

The List returned by the GET method is supposed to have 4 CustomObject's in it and it seems like the JSON response has that at least. Each CustomObject has variables like 'Name', 'Date', 'Time', etc. I've tried printing data in my HTML using {{selectedConfiguration[0].Name}} and it prints out nothing. I'm not really sure what do to from here (or if I'm doing something wrong) in order to display each object's info.

2 Answers 2

3

The short answer

In your example, you're trying to convert a JSON object to a string. To get it to work, you will probably have to do:

this.selectedConfiguration = JSON.stringify(res);

The long answer

Let's say your CustomObject class looks something like:

public class CustomObject
{
    public string Name { get; set; }
    public DateTime Date { get; set; }
}

In your Angular code, create an interface that mimics it. Verify in developer tools that the case matches ("Name" and not "name"):

export interface ICustomObject {
    Name: string;
    Date: Date;
}

Leverage the type checking of TypeScript and actually read your returned list as an array of your CustomObjects:

export class Home implements OnInit {
  public configurations: ICustomObject[];
  public selectedConfiguration: ICustomObject;

  constructor(private http: Http) {
    // move initialization code to ngOnInit. Don't forget the import and implements
  }

  ngOnInit() {
    this.getConfigurations()
        .subscribe((customList: ICustomObject[]) => {
            this.configurations = customList;
            // todo: determine the selected configuration
            this.selectedConfiguration = this.configurations[0];
        });
  }

  getConfigurations(): ICustomObject[] {
    return this.http.get('atr/testing')
        .map((response: Response) => {
            return <ICustomObject[]>response.json();
        });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Return it as a json result:

[HttpGet("/atr/testing")]
public JsonResult GetCustomObjects()
{
    //MasterObject.CustomObjects returns a List of CustomObjects
    return Json(MasterObject.CustomObjects());
}

2 Comments

Thanks for the reply. I changed the function to return a JsonResult but it still won't print. I'm using Chrome's developer tools and the Response is definitely returning a JSON representation of the List - this seems to be a JSON object. Do you know how I would go about accessing the different parts of the JSON in my HTML file?
So you receive a json as response that is good. console.log shows it as [object] because it is a complex object. to loop through you array in angular you need to use ng-repeat.

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.