为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
蝴蝶不菲 2023-07-22 16:06:30
我不理解 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}
查看完整描述

2 回答

?
小唯快跑啊

TA贡献1863条经验 获得超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 });


}



查看完整回答
反对 回复 2023-07-22
?
慕斯709654

TA贡献1840条经验 获得超5个赞

添加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 });


    }


查看完整回答
反对 回复 2023-07-22
  • 2 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信