Struggling to retrieve posted data using Angular with asp.net

I have encountered an issue while sending a post request from Angular to my ASP.NET server. I am trying to access the values of my custom model class (SchoolModel) and I can see that all the values are correct inside Angular. However, when I attempt to retrieve these values in ASP.NET, nothing is returned from inside the object. Can someone please assist me if there is something I am overlooking?

Here is my Angular request code:

const headers = {'Content-Type': 'application/json', };
            const body = { schoolData };
            this.http.post<any>(this.baseUrl + "Api/CreateSchool", body, { headers }).subscribe(response => {
            
                console.log(response);
                
    
            }, error => console.error(error));

This is my ASP.NET code:

    [HttpPost]
    [Route("Api/CreateSchool")]
    public bool CreateNewSchool(SchoolModel schoolData)
    {
        bool schoolCreated = false;
        // IT PRINTS SCHOOL NAME ONLY 
        Console.WriteLine("SCHOOL NAME " + schoolData.Name);
        return schoolCreated;
    }

Answer №1

To utilize the FromBody attribute, you can employ it in the following manner:

void AddNewCollege([FromBody]CollegeInfo collegeDetails) {

Answer №2

Using JSON.stringify(body) was the key to getting it to function

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

Problem with npm link <package>

Operating System: Windows 7, 64-bit Npm Version: 3.10.10 Node Version: 6.9.4 Hello, We are currently encountering issues with a custom npm package that we have published on our registry and installed in our Angular applications. Below are the di ...

There seems to be an issue with my React application that was built using Webpack 5 and compiled with TypeScript. The @tailwind directive is not functioning properly in the browser, and

As I embark on creating a fresh react application using Webpack 5, Tailwind CSS, and Typescript, I find myself at a crossroads. Despite piecing together various tutorials, I am struggling to configure the postcss-loader for Tailwind. While traditional .css ...

Protected HTTP Live Streaming 128-bit AES encryption key location

After encrypting a video using HLS AES-128 with the apple tool, I have generated an m3u8 file that looks like this: #EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-KEY:METHOD=AES-128,URI="https://xxxxx.com/api/ ...

retrieving information from an array nested within a JSON object in an Angular application

I am struggling to retrieve two specific values from a JSON object. The content of the JSON is as follows: [ { "type":"session_start", "properties":[ { "property":"activity&q ...

In my Angular 6 project, I am faced with the challenge of having two separate navbars and routes that need to be integrated into the same page

I have two different navigation bars that I need to configure. The first one is the primary navbar, while the second one is for a specific section of the project. I also need to ensure that the second navbar remains visible when a link in the footer is cli ...

There was an error encountered trying to access the options (URL) with a 405 method not allowed status. Additionally, the request to load the (URL) failed with a response indicating an

I attempted to retrieve data from an API using httpClient in Angular 5, but encountered errors. Below are the issues I faced: 1) ERROR: when trying to access http://localhost:8080/api/getdata, I received a 405 error (method not allowed). 2) ERROR: failed t ...

How can we ensure in ReactJS that one API call has been completed before making another one?

How can we ensure one API call is completed before making the next call in reactJS? In my componentDidMount function, I need to check the length of data. When the length is more than 4, I first want to save the data and then retrieve it. componentDidM ...

Importing JSON data into a WPF ListBox

Exploring WPF for the first time and enjoying it, but encountering an issue. I have successfully set up a class to save and load root names and child information in JSON format. I am trying to load the JSON nickname items into a listbox when the program st ...

Enriching HtmlElement with a type property using TypeScript

Check out my TypeScript code snippet below: let script: HTMLElement = document.createElement("script"); script.appendChild(document.createTextNode(` { "json": "property" } `)); script.id = 'appContextModel'; document.body.appendChild(scr ...

Tips for setting default values for named parameters in JavaScript

In my TypeScript method, I am using named parameters like this public foo({x, y, z , m , n} : {x:string, y: number, z: number, m?:string, n?:number}) { } The parameters m and n will be provided from another object like const defaults = { m : 'M&apo ...

Struggling to pass images from express to Angular6

Snippet of Node.js code app.use('/static/images',express.static(path.join(__dirname,'images'))) Folder Structure |-root | -images When attempting to fetch data in my Angular6+ application, I am utilizing the following ...

How to implement a responsive menu using the onPress attribute of TouchableOpacity

Looking to implement a profile picture upload feature with the ability to choose between getting an image from the camera (using getMediaFromCamera) or selecting one from the gallery (using getMediaFromImageLibrary). I currently have a TouchableOpacity set ...

There seems to be a troublesome character in the Nuxt3 production server causing some issues

When submitting an HTML encoded text to the server, everything runs smoothly on the development environment. However, once it is deployed to a Netlify server, the same request triggers a 500 error and the server side logging middleware only recognizes a PO ...

Ways to retrieve an image uploaded on a nestjs server

I am facing an issue with my NestJS server where I have uploaded images but cannot access them as they appear to be some unreadable data. I have tried converting them to a blob and then setting it as an object URL for the image tag, but that also did not w ...

How to handle Json parsing without arrays in C#

After making a call to an API, I receive the following JSON data { "services":{ "Home":{ "Homecall":{ "id":"10" }, "Two Day":{ "id":& ...

What methods can I use to prevent the addition of new values and duplicates within angular mat-chips?

I am utilizing angular 9 mat-chips and I am seeking a way to prevent adding new values in the input field, allowing only items from the autocomplete list to be added. For instance, if 'abc' is typed, which is not in the autocomplete list, pressin ...

Upgrade your development stack from angular 2 with webpack 1 to angular 6 with webpack 4

Recently, I have made the transition from Angular 2 and Webpack 1 to Angular 6 and Webpack 4. However, I am facing challenges finding the best dependencies for this new setup. Does anyone have any suggestions for the best dependencies to use with Angular ...

JavaScript validation controls do not function properly when enabled on the client side

Following the requirements, I have disabled all validation controls on the page during the PageLoad event on the server side. When the submit button is clicked, I want to activate the validations and check if the page is valid for submission. If not, then ...

Importing dynamically into Ionic 2 from locations other than the "node_modules" directory

I've recently reviewed the documentation for ModuleResolution in TypeScript on this page: https://www.typescriptlang.org/docs/handbook/module-resolution.html#node My understanding is that all files I wish to import must reside within the node_modules ...

Decorators in Angular 9 do not have the capability to support function expressions

Currently, I am working with Angular 9 and facing an issue while generating dynamic routes values during runtime. I have implemented a ComplexUrlRouter to achieve this functionality and integrated it into my Route configuration. However, I encountered the ...