数据注释不适用于 [必需(错误消息 = ......)]

我不理解 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

}


蝴蝶不菲
浏览 111回答 2
2回答

小唯快跑啊

您只需在Home Controller的CreateContact操作中添加ModelState 验证即可。[HttpPost]public ActionResult CreateContact(CreateContactStepOne contact){        // add this section to the top of your action        if(!ModelState.IsValid)        {           return View("viewName", contact);        }        person p = new person();        p.FirstName = contact.FirstName;        p.LastName = contact.LastName;        if (contact.PhoneNumber == Phone.Home)        {            p.HomePhone = contact.ContactPhoneNumber.ToString();        }        else if (contact.PhoneNumber == Phone.Mobile)        {            p.MobilePhone = contact.ContactPhoneNumber.ToString();        }        else if (contact.PhoneNumber == Phone.Office)        {            p.OfficePhone = contact.ContactPhoneNumber.ToString();        }        PhonebookEntities db = new PhonebookEntities();        db.people.Add(p);        db.SaveChanges();        //Redirect to ActionMethod ContactDetails and passes the personID as parameter        return RedirectToAction("AddContactDetails", new { id = p.PersonID });}

慕斯709654

添加if(!ModelState.IsValid)return View(contact);到CreateContact操作[HttpPost]    public ActionResult CreateContact(CreateContactStepOne contact)    {            if(!ModelState.IsValid)return View(contact);            person p = new person();            p.FirstName = contact.FirstName;            p.LastName = contact.LastName;            if (contact.PhoneNumber == Phone.Home)            {                p.HomePhone = contact.ContactPhoneNumber.ToString();            }            else if (contact.PhoneNumber == Phone.Mobile)            {                p.MobilePhone = contact.ContactPhoneNumber.ToString();            }            else if (contact.PhoneNumber == Phone.Office)            {                p.OfficePhone = contact.ContactPhoneNumber.ToString();            }            PhonebookEntities db = new PhonebookEntities();            db.people.Add(p);            db.SaveChanges();            //Redirect to ActionMethod ContactDetails and passes the personID as parameter            return RedirectToAction("AddContactDetails", new { id = p.PersonID });    }
打开App,查看更多内容
随时随地看视频慕课网APP