Reputation: 19903
I have a model, like this :
public class ContactDto
{
public int Id { get; set; }
[Display(Name = "FirstName")]
public string FirstName { get; set; }
[Required]
[Display(Name = "LastName")]
[StringLength(50)]
public string LastName { get; set; }
}
when I do this :
if (!ModelState.IsValid)
{
}
The model is not valid because, I have 0 in the Id. When I do the sume but an update, means with an idea, no problem. Why the Id is check ? How avoid this ?
Thanks,
Upvotes: 1
Views: 553
Reputation: 4120
An integer in the viewmodel is automatically required because it is not nullable. The default value of an integer is 0, the model will be invalid when id is set to 0.
Try defining the id like this in your viewmodel:
public int? Id { get; set; }
Upvotes: 3