2 回答
TA贡献1786条经验 获得超13个赞
您不应将实体用作输入/输出参数。而是创建两个单独的视图模型来表示被调用的动作:
public class User
{
public long Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
// When creating, the client cannot know the Id because it doesn't exist
public class CreateUserViewModel
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
// and when updating, the Id is required but not the Email nor the Password
public class UpdateUserViewModel
{
[Required]
public long Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
当然,由于您正在创建API,因此“视图模型”可能没有多大意义。您可以改用数据传输对象(DTO)术语。
- 2 回答
- 0 关注
- 129 浏览
添加回答
举报