我不理解 ModelState.IsValid,我相信这就是我在 CreateContact Action 方法中所缺少的。请解释一下,我应该如何检查 If(ModelState.IsValid)?我的验证不起作用。如果文本框(或模型属性)为空,我应该看到一条错误消息。如果它符合验证规则,我应该能够将日期保存到数据库并在单击提交按钮后重定向到“AddContactDetails”操作方法。
查看模型
public class CreateContactStepOne
{
public int PersonID { get; set; }
[Required(ErrorMessage = "Nickname is required and keep it short upto 10 characters")]
[Display(Name = "Nickname:")]
[RegularExpression("^[a-zA-Z. ]{1,10}$", ErrorMessage = "Only letters and no numbers or special characters allowed. Also, limit your First Name to 10 character length.")]
public string NickName { get; set; }
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name:")]
[RegularExpression("^[a-zA-Z. ]{1,25}$", ErrorMessage = "Only letters and no numbers or special characters allowed. Also, limit your First Name to 25 character length.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[Display(Name = "Last Name: ")]
[RegularExpression("^[a-zA-Z. ]{1,25}$", ErrorMessage = "Only letters and no numbers or special characters allowed. Also, limit your Last Name to 25 character length.")]
public string LastName { get; set; }
[Required(ErrorMessage = "Select Phone type and enter a phone number below")]
[Display(Name = "Phone: ")]
//[RegularExpression("^[0-9-]{1,12}$", ErrorMessage = "Please enter the correct format. Example 717-123-4567")]
public Phone PhoneNumber { get; set; }
[Required(ErrorMessage = "Phone number is required")]
[RegularExpression(@"^\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$", ErrorMessage = "Please enter the correct format. Example (717) 123-4567")]
public string ContactPhoneNumber { get; set; }
}
public enum Phone
{
Mobile, Office, Home
}
小唯快跑啊
慕斯709654
相关分类