Struggling to retrieve the ID from the API within the Angular and .NET Core component

Currently, I am working on a test project to enhance my knowledge of Angular. However, I have encountered an issue where the student's id fetched from the service is null. To handle the data, I have implemented a StudentController. Below is a snippet of code showing how I retrieve student information:

// GET: api/students
    [HttpGet]
    public IActionResult GetAll()
    {
        var students = _unitOfWork.Students.GetAll();
        return Ok(students);
    }

Furthermore, here is my domain model (Student.cs) with the UnitOfWork method - GetAll():

public class Student
{
    public int StudentId{get;set;}
    public string Name { get; set; }
    public DateTime bDate {get;set;} 
}

The implementation of GetAll() in Repository.cs:

public virtual IEnumerable<TEntity> GetAll()
    {
        return _entities.ToList();
    }

In addition, I have created a service to interact with the API and fetch data (student.service.ts):

private readonly _StudentsUrl: string = "/api/students";
GetAll()
{
    return this.http.get(this._StudentsUrl)
    .map(res => res.json());
  }

However, when displaying the data on the client-side, the student's id is undefined (students.component.ts)

export class StudentsComponent {
    students:Student[];
    constructor(private studentService: StudentService) { }

    ngOnInit() {
      this.studentService.GetAll()
      .subscribe(students => this.students = students);
    }
}
export class Student {
    StudentId: number;
    Name: string;
    bDate:DateTimeFormat
}

While rendering the data in student.component.html, the name and bDate values are shown properly but not the StudentId.

<tr *ngFor="let st of students">
    <td>{{ st.StudentId }}</td>
    <td>{{ st.name }}</td>
    <td>{{ st.bDate }}</td>
    <td>
      <a [routerLink]="['/students/', st.StudentId]">View</a>
    </td>
  </tr>

I have tested the functionality of my StudentController using Postman, and it seems to be passing data correctly.

Where could I possibly be making a mistake?

Answer №1

It may seem unusual, but I managed to solve the issue by changing 'StudentId' to 'studentId'. After making this adjustment in the HTML code:

<td>{{ st.StudentId }}</td>

I switched it to:

<td>{{ st.studentId }}</td>

Here is the updated code snippet:

<tr *ngFor="let st of students">
        <td>{{ st.studentId }}</td>
        <td>{{ st.name }}</td>
        <td>{{ st.bDate }}</td>
        <td>
            <a [routerLink]="['/students/', st.studentId]">View</a>
        </td>
    </tr>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to effectively work with ArrayForm when trying to include additional form fields

I'm attempting to add a playlist in Mat-dialog that contains songs in a list using (formArray) as shown here: https://i.stack.imgur.com/diWnm.png However, I am unsure of the mistake I might be making This is how my dialogue appears: https://i.stac ...

Angular 7 service utilizes HttpClient to retrieve stock data in the component's TypeScript file

Why am I getting an undefined result? I am using a contact API in my service to fetch data for the component HTML, but when I log the result, it is coming up as undefined. element: any ; constructor(private service: BlockService) { } ngOnInit() ...

Execute a function when a button is pressed in a React application

Currently, I am dynamically generating some HTML and have a requirement for certain "events" to trigger an onclick function. The technology stack I am using for this project involves React and TypeScript. My initial approach is as follows: function add_ev ...

How can Typescript help enhance the readability of optional React prop types?

When working with React, it is common practice to use null to indicate that a prop is optional: function Foo({ count = null }) {} The TypeScript type for this scenario would be: function Foo({ count = null }: { count: number | null }): ReactElement {} Wh ...

Is it possible for ko.mapping to elegantly encompass both getters and setters?

Exploring the fusion of Knockout and TypeScript. Check out this code snippet: class Person { public FirstName:string = "John"; public LastName: string = "Doe"; public get FullName(): string { return this.FirstName + " " + this.Las ...

Tips for positioning buttons within the MdDialog md-dialog-actions section

Can a button be aligned on the left in the MdDialog's md-dialog-actions block, while having two buttons aligned to the right? If you check out this example of what I'm working on, how can I separate the Yes and No buttons in the first modal? (Re ...

Exploring the integration of Server-Sent Events in Angular

I'm currently working on incorporating Server-Sent Events (SSE) into my testing application. The server side configuration has been completed, and I am utilizing the endpoint (api/v1/sse/document). The aim here is that whenever a scan is performed, I ...

How can I determine the appropriate data type for 'this' when utilizing TypeScript in conjunction with DataTables?

I found some code on a page I visited: https://datatables.net/reference/api/columns().every() Here is the specific code snippet I am using: var table = $('#example').DataTable(); table.columns().every( function () { var that = this; ...

Value returned by Typescript

I have been attempting to retrieve a string from a typescript function - private _renderListAsync(): string { let _accHtml: string=''; // Local environment if (Environment.type === EnvironmentType.Local) { this._getMockLi ...

A more concise approach to accessing a generic class method in Typescript

Let's analyze the code snippet below: export class BaseClass<T> { property = this.buildProperty(); buildProperty(){ return someBuilder<T>(); } } Through TypeScript, the type of property is automatically determined based on the ...

Encountering problem while setting up OpenTelemetry integration with Honeycomb

Currently, I am endeavoring to implement honeycomb into my .NET sample application utilizing .NET 6. Upon running my code, I encountered the following exception: System.MissingMethodException: 'Method not found: 'OpenTelemetry.Trace.TracerProvi ...

Access the Ionic2 App through the Slider option

Trying out Ionic 2 and facing an issue. Created a default side-menu app from CLI with a slider. Need to start the actual side-menu app from the last slide on button click or anchor link. My app.ts: @Component({ templateUrl: 'build/app.html' }) ...

How to Insert a New User into the Database Using Angularfire2

During the login process, I implemented the following method: login() { this.auth.login(); this.authService.login().subscribe(() => { if (this.authService.isLoggedIn) { console.log("ADDING THE USER.."); // Insert a new user into the use ...

"Adjusting the position of an Ionic Menu on-the-fly

As I strive to update the Ionic 3 Menu side dynamically when the user changes the language, a challenge arises for RTL languages where the menu needs to be on the right instead of the default left. To tackle this issue, I have subscribed to the TranslateS ...

Angular error: Unable to access the 'toLowerCase' property of an undefined value

I've been working on creating my own custom filter pipe by following the instructions in this video tutorial, but I encountered an error message stating, "Angular, TypeError: Cannot read property 'toLowerCase' of undefined". I have already i ...

Creating a new object from a class type in C#

Trying to achieve this specific functionality: Class<?> c = Class.forName(dir.substring(6).replaceAll("/", ".") + file.getName().replaceAll(".java", "")); Packet packet = (Packet)c.newInstance(); If you're familiar with ...

Using Angular's routerLink feature to assign a specific outlet (version 5.0.2)

Despite reading numerous posts on this problem, none of the solutions seem to work for me. I am working with one app module, one routing module, and no additional modules. The issue I'm facing is that... Standard routes can be linked from any compo ...

How to effectively utilize API references in TypeScript within Angular 7

Greetings, I am new to working with AngularJS and I am currently attempting to customize the appearance of a particular library. If you are interested, here is the link to the library: https://github.com/sanjayV/ng-image-slider (API references can be foun ...

Setting a default value dynamically in a `select` control can be done by using JavaScript to

Upon subscribing to the HTTP server for data retrieval, my select control in Angular framework gets loaded with the received data. My objective is to set a default value that comprises three values from the server object separated by slashes ("/"), which r ...

Error encountered when attempting to embed a SoundCloud player in Angular 4: Unable to execute 'createPattern' on 'CanvasRenderingContext2D' due to the canvas width being 0

I attempted to integrate the SoundCloud iframe into my Angular 4 component, but encountered the following error message: Failed to execute 'createPattern' on 'CanvasRenderingContext2D': The canvas width is 0. Here is the iframe code ...