简体   繁体   English

空数据从Angular 5传递到Asp.NET MVC控制器

[英]Null Data is passed from Angular 5 to Asp.NET MVC Controller

When I am passing data from Angular 5 to Asp.Net MVC Controller. 当我将数据从Angular 5传递到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. 当内容类型更改为“ application / x-www-form-urlencoded”时 ,控件将传递给控制器​​,但数据为空。 but for json/application it will not pass control to API if it is with [Httppost] attribute. 但对于json / application,如果具有[Httppost]属性,则不会将控制权传递给API。

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. 不会从此http调用传递数据,其中User是我在Angular 5中创建的MODEL。

[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 这是我的MVC控制器

The Model class for Angular 5 and Asp.Net MVC are Same. Angular 5和Asp.Net MVC的Model类相同。

You should add [FromBody] attribute to your UserModel input 您应该将[FromBody]属性添加到UserModel输入中

[HttpPost]
public ActionResult Registration([FromBody] UserModel usermodel)
{
    ////
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM