Home > Net >  Unable to get posted data with Angular from asp.net
Unable to get posted data with Angular from asp.net

Time:05-26

I'm sending a post request to my asp.net server from angular and i tried printing the values of my custom model class (SchoolModel) and it's all the values corrent inside angular. But when i try to get these values on asp.net it doesn't return anything from inside the object. Please help me if i'm missing something.

My angular code request

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

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

CodePudding user response:

Use FromBody attribute.

public bool CreateNewSchool([FromBody]SchoolModel schoolData) {

CodePudding user response:

I had to use JSON.stringify(body) to make it work

  • Related