Json,Web API,绑定,绑定模型,AutoMapper

时间:2017-08-22 07:54:10

标签: asp.net json rest binding

我需要一些关于这个问题的建议。这是我的POST方法,我应该通过正文传递Json对象,但它给了我这个错误.. 就像..我已经尝试过我发现的每一个例子[FromUri],[FromBody],你能帮我解决这个问题吗?

     // POST api/Users

            public void Post([FromUri] UsersBindingModels userBindingModel)
            {
                Users user = new Users();
                var newUser = Mapper.Map<UsersBindingModels>(user);
----------->     newUser = // I think I have to parse it here 
                _repository.Insert(newUser);
                _repository.Save();
            }

我的错误是: 参数1:无法转换为&#39; BindingModels.UsersBindingModels&#39;到&#39; Models.Users&#39;

//Model
    public class Users : BaseModel
    {
        [Required]
        public string Name { get; set; }
        [Unique]
        [Required]
        public string Email { get; set; }
        [Required]
        public string Password { get; set; }
        public bool IsTeacher { get; set; }

        public virtual List<Courses> Courses { get; set; }
    } 


//BindingModel
    public class UsersBindingModels
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public bool isTeacher { get; set; }

        public virtual List<Courses> courses { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

您必须保存存储库中的更改。此代码可能会帮助您:

public IActionResult Post([FromBody] UsersBindingModels userBindingModel)
            {
                var newUser =_repository.Insert(userBindingModel);
                return Ok(newUser);
            }