【发布时间】:2020-07-24 12:43:06
【问题描述】:
我正在尝试以 JSON 格式发送一个对象,但该方法总是得到一个空对象
这是错误:
Server Error in '/' Application.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Source Error:
Line 15: context = ApplicationDbContext.Create();
Line 16: context.Users.Add(user);
Line 17: if (context.SaveChanges() == 0)
Line 18: {
Line 19: context.Dispose();
Source File: D:\Education\Git\Online-Store-Platform-API\OnlineStorePlatform\OnlineStorePlatform\DBContext\UserContext.cs Line: 17
这是我的代码:
public class AccountController : Controller
{
//public UserDTO user;
public UserContext myContext;
// GET: Account
public ActionResult Index()
{
return View();
}
//[HttpPost]
public ActionResult Register([FromBody]UserDTO user)
{
//user = new UserDTO(email, password, userName);
myContext = new UserContext();
if (myContext.addUser(new User(user)) == null) return Content("ERROR");
return Content("Created Successfully");
}
}
当我发送一个 UserDTO 对象时,总是得到 null
我的要求:
使用DTO类:
public class UserDTO
{
public String email, userName, password;
public UserDTO(String email, String password, String userName)
{
this.email = email;
this.password = password;
this.userName = userName;
}
public UserDTO() { }
}
当我尝试调试时,我得到了一个空对象
【问题讨论】:
-
对于
email, userName, password使用带有getter 和setter 的属性,例如public string email{get;set;}
标签: c# asp.net json asp.net-mvc