0
이 문제에 대해 몇 가지 질문이 있습니다. 그러나 이제는 코드를 다시 작성하고 거의 모든 코드가 작동합니다. 유일한 문제는 for가 제출 된 후에 모델 상태를 확인하지 않는 것입니다. 양식이 성공적 일 때에도 오류가 표시되기 때문입니다. 여기 내 코드가있다.양식이 제출 후 모델 상태 또는 양식 지우기를 점검하지 않습니다.
[HttpPost]
public ActionResult ContactForm(ContactModel emailModel)
{
MailMessage oMail = new MailMessage();
oMail.From = new MailAddress("[email protected]", "Web Contact Form");
oMail.To.Add("[email protected]");
oMail.Subject = emailModel.Subject;
string body = "Name: " + emailModel.Name + "\n"
+ "Email: " + emailModel.Email + "\n"
+ "Phone: " + emailModel.Phone + "\n\n"
+ "Company: " + emailModel.Company + "\n"
+ "Website: " + emailModel.Website + "\n"
+ emailModel.Message;
oMail.Body = body;
SmtpClient client = new SmtpClient("smtpout.secureserver.net");
client.Credentials = new NetworkCredential("username", "password");
client.Send(oMail);
string message = "There are a few errors";
if (ModelState.IsValid)
{
message = "Thanks! We'll get back to you soon.";
ModelState.Clear();
}
if (Request.IsAjaxRequest())
{
return new JsonResult { ContentEncoding = Encoding.UTF8, Data = new { success = true, message = message } };
}
TempData["Message"] = message;
return View();
}
나는 내 자신의 대답을 발견했다. "If (ModelState.IsValid)"를 다시 배열했습니다. –
[HttpPost] 공개 ActionResult ContactForm (ContactModel emailModel) { string message = "몇 가지 오류가 있습니다."; if (ModelState.IsValid) { MailMessage 내용 message = "감사합니다. 곧 연락 드리겠습니다."; ModelState.Clear(); } –