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 });
}
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 });
}
- 2 回答
- 0 关注
- 110 浏览
添加回答
举报