亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據注釋不適用于 [必需(錯誤消息 = ......)]

數據注釋不適用于 [必需(錯誤消息 = ......)]

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 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號