0

When I am passing data from Angular 5 to Asp.Net MVC Controller. it is receiving null data. when content type is changed to "application/x-www-form-urlencoded" the control is passed to controller but with null data. but for json/application it will not pass control to API if it is with [Httppost] attribute.

adduser(userdata: User) {
var body = JSON.stringify(userdata);
alert(JSON.stringify(userdata));
return this.httpClient.post("http://localhost:21936/UserRegistration/Registration",body,httpOptions);
}

Data is not passed from this http call, where User is my MODEL which o created in Angular 5.

[HttpPost]
public ActionResult Registration(UserModel usermodel)
{
    using (var dbcontext = new ModelDB())
    {
        UserModel usermodelobj = new UserModel()
        {
            id = usermodel.id,
            name = usermodel.name,
            age = usermodel.age,
            department = usermodel.department,
            mobile = usermodel.mobile,
            email = usermodel.email,
            password = usermodel.password

        };
        dbcontext.userModel.Add(usermodelobj);
        dbcontext.SaveChanges();
    }
    return View();
}

This my MVC Controller

The Model class for Angular 5 and Asp.Net MVC are Same.

2
  • Have you tried [FromBody] without stringify and with application/json as content type? Commented Apr 4, 2018 at 17:26
  • Yeah, I tried still passing Null values.In console it is showing error 1. Response for preflight has invalid HTTP status code 500. 2. {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …} Commented Apr 5, 2018 at 4:13

1 Answer 1

0

You should add [FromBody] attribute to your UserModel input

[HttpPost]
public ActionResult Registration([FromBody] UserModel usermodel)
{
    ////
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not Working, still Null data.

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.