第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

C#
蝴蝶不菲 2023-07-22 16:06:30
我不理解 ModelState.IsValid,我相信這就是我在 CreateContact Action 方法中所缺少的。請解釋一下,我應(yīng)該如何檢查 If(ModelState.IsValid)?我的驗(yàn)證不起作用。如果文本框(或模型屬性)為空,我應(yīng)該看到一條錯誤消息。如果它符合驗(yàn)證規(guī)則,我應(yīng)該能夠?qū)⑷掌诒4娴綌?shù)據(jù)庫并在單擊提交按鈕后重定向到“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貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個贊

您只需在Home Controller的CreateContact操作中添加ModelState 驗(yàn)證即可。


[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 });


}



查看完整回答
反對 回復(fù) 2023-07-22
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超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 });


    }


查看完整回答
反對 回復(fù) 2023-07-22
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號