【发布时间】:2018-03-31 16:38:04
【问题描述】:
我是 MVC 的初学者,最近开始使用 Web api 开发一个测试站点,我的 api 控制器名称为 CustomerController,它返回简单的 IEnumerable 客户列表。
当我使用 POSTMAN 测试我的 api 时,它的显示记录但 Id 显示 0 (id=0) 下面是供参考的图像 [ID 显示 0][1] [1]:https://i.stack.imgur.com/4MqUb.png
below is my api/controller
public class CustomersController : ApiController{
private ApplicationDbContext _context;
public CustomersController(){
_context = new ApplicationDbContext();
}
//GET api/Customer
public IEnumerable<CustomerDto> GetCustomers(){
return _context.Customer.ToList().Select(Mapper.Map<Customer,CustomerDto>);
}
>CUstomer MODEL
public class Customer
{
public int id{ get; set; }
[Required(ErrorMessage = "Please enter your name")]
[StringLength(255)]
public string Name{ get; set; }
public bool IsSubscribeToNewsLetter{ get; set; }
public MembershipType MembershipType{ get; set; }
[Display(Name = "Membership Type")]
public byte MembershipTypeId{ get; set; }
[Display(Name = "Date of Birth")]
[Min18YearsIfMember]
public DateTime? BirthDate{ get; set; }
}
}
>CUSTOMERDTO MODEL
public class CustomerDto
{
public int id { get; set; }
[Required(ErrorMessage = "Please enter your name")]
[StringLength(255)]
public string Name { get; set; }
public bool IsSubscribeToNewsLetter { get; set; }
public byte MembershipTypeId { get; set; }
//[Min18YearsIfMember]
public DateTime? BirthDate { get; set; }
}
<i/>
【问题讨论】:
-
很抱歉,我附上了电影列表图片,但同样的问题发生在客户身上也同样的问题
-
你能展示一下 Customer 和 CustomerDto 模型吗?是否有可能 automapper 找不到 Id 的正确匹配项?
-
@amburt05 请查看我编辑的问题版本,DTO 和模型中的字段名称相同
标签: jquery json rest api model-view-controller