Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 表单未在提交后检查模型状态或清除表单_Asp.net Mvc_Asp.net Mvc 2_Asp.net Mail - Fatal编程技术网

Asp.net mvc 表单未在提交后检查模型状态或清除表单

Asp.net mvc 表单未在提交后检查模型状态或清除表单,asp.net-mvc,asp.net-mvc-2,asp.net-mail,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mail,关于这个问题,我有几个问题。然而,现在我已经重做了我的代码,几乎所有的代码都在工作。唯一的问题是在提交for之后,它没有检查模型状态,因为即使表单成功,它也会显示有错误。这是我的密码 [HttpPost] public ActionResult ContactForm(ContactModel emailModel) { MailMessage oMail = new MailMessage(); oMail.From = new MailAdd

关于这个问题,我有几个问题。然而,现在我已经重做了我的代码,几乎所有的代码都在工作。唯一的问题是在提交for之后,它没有检查模型状态,因为即使表单成功,它也会显示有错误。这是我的密码

[HttpPost]
    public ActionResult ContactForm(ContactModel emailModel)
    {
        MailMessage oMail = new MailMessage();

        oMail.From = new MailAddress("no-reply@hovdenoil.com", "Web Contact Form");
        oMail.To.Add("email@hovdenoil.com");
        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)放得太早了。Hear是我最后一个有效的代码

[HttpPost]
public ActionResult ContactForm(ContactModel emailModel)
{
    string message = "There are a few errors";

    if (ModelState.IsValid)
    {

    MailMessage oMail = new MailMessage();

    oMail.From = new MailAddress("no-reply@hovdenoil.com", "Web Contact Form");
    oMail.To.Add("email@hovdenoil.com");
    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);

        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]公共操作结果ContactForm(ContactModel-emailModel){string message=“有一些错误”;If(ModelState.IsValid){MailMessage content message=“谢谢!我们很快会回复您的。”ModelState.Clear();}