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.
You should add [FromBody]
attribute to your UserModel
input
[HttpPost]
public ActionResult Registration([FromBody] UserModel usermodel)
{
////
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.