0
我是MVC的初学者,最近开始使用web apis开发测试网站,我有我的api控制器名称CustomerController,它返回简单的IEnumerable客户列表。WebApi Json Id不检索所有其他字段显示在哪里
当我测试API使用邮差其示出了记录,但1d示出了0(ID = 0)下面参考 [IDS表示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/>
我很抱歉我附上电影列表图像,但同样的问题发生在客户也出现同样的问题 –
你可以显示客户和CustomerDto模型?有没有可能automapper找不到Id的正确匹配? – amburt05
@ amburt05请参阅我编辑的问题版本,字段名称在DTO和模型中都相同 –