I am using System.ComponentModel.DataAnnotations for model binding and validate my ASP.NET MVC application.
I have a C# model class like this
public class Employee
{
public Int64 EmployeeId { get; set; }
[Required]
public string FullName { get; set; }
[Required]
public string Username { get; set; }
public string Password { get; set; }
[Compare("Password")]
public string ConfirmPassword { get; set; }
}
When I submit the form via AJAX to insert a record, the values are set to Employee
object correctly.
But ModelState.IsValid
statement is false always and it says The EmployeeId field is required .
Since EmployeeId
is an identity column in the database, I do not set it in controller.
How can I fix this issue?
When I update a record, I need to make EmployeeId
as required. How can I handle this also?
I think simplest/cleanest is to make EmployeeId
nullable:
public class Employee
{
public Int64? EmployeeId { get; set; }
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.