我的帖子中ModelState.IsValid为false

时间:2014-07-29 07:18:58

标签: asp.net-mvc asp.net-mvc-4

这是我的控制者:

    [HttpPost]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EMail(SendMailData objModelMail, HttpPostedFileBase fileUploader,SendMailData smd)
    {
        //string Emailid = smd.To;
        //TryUpdateModel<SendMailData>(smd);
        //var result = new List<ValidationResult>();
        //var Context = new ValidationContext(ModelState, null, null);
        //var errors = ModelState.Where(v => v.Value.Errors.Any());
        if (ModelState.IsValid)
        {
        string from = "MYMAILID";
        using (MailMessage mail = new MailMessage(from, objModelMail.To))
        {
            mail.Subject = objModelMail.Subject;
            mail.Body = objModelMail.Body;
            if (fileUploader != null)
            {
                string Filename = Path.GetFileName(fileUploader.FileName);
                mail.Attachments.Add(new Attachment(fileUploader.InputStream, Filename));
            }

            //mail.To.Add(new MailAddress(eMailBox.Text));
            // mail.To.Add(objModelMail.To);
            //mail.From = new MailAddress(objModelMail.From);
            //mail.Subject = objModelMail.Subject;
            //string body = objModelMail.Body;
            //mail.Body = body;
            mail.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient("127.0.0.1", 25);
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("MYMAILID", "PASSWORD",from);
            smtp.Port = 587;
            smtp.ServicePoint.MaxIdleTime = 1;
            smtp.Host = "smtp.gmail.com";
            smtp.Send(mail);
            ViewBag.message = "Send";
            mail.Dispose();
            return View("EMail", objModelMail);
        }
        }
        else
        {
            return View();
        }
    }    

这是我的模特:

public class SendMailData
{
    [Required(ErrorMessage = "You Can't Leave This Empty")]
    [RegularExpression(@"^(([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,7})([,;]\W?(?!$))?)+$", ErrorMessage = "Please enter correct email address")]
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}    

这是我的观点:

@model SampleUniversity1.Models.SendMailData
@{
    ViewBag.Title = "EMail";
}
<script src="~/Scripts/jquery-1.7.1.min.js">
</script>
<script type="text/javascript">
    if ('@ViewBag.message' == 'Send') {
        alert("Email Sended Successfully");
    }
</script>
<h2>Mail</h2>
<fieldset>
    <legend>
        Send Mail
    </legend>
    @using (Html.BeginForm("EMail", "Student", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
    { 
        @*@Html.AntiForgeryToken();*@
        @Html.ValidationSummary(true)
        <input type="submit" value="Send" style="width: 100px;" />
        <table>
            <tr>
                <td>
                    To
                </td>
                <td>
                    @Html.TextAreaFor(x => x.To)
                    @Html.ValidationMessageFor(x => x.To)
                </td>
            </tr>
            <tr>
                <td>
                    Subject
                </td>
                <td>
                    @Html.TextAreaFor(x => x.Subject)
                </td>
            </tr>
            <tr>
                <td>
                    Attachment
                </td>
                <td>
                    <input type="file" name="fileUploader" />
                </td>
            </tr>
            <tr>
                <td>
                    Body
                </td>
                <td>
                    @Html.TextAreaFor(x => x.Body)
                </td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
    }
</fieldset>

当我输入所有正确的值时,它将不会显示任何错误,否则部分正在处理 else部分是执行ModelState.IsValid是false。

1 个答案:

答案 0 :(得分:1)

RequiredAttribute属性上有From,但在您查看时我看不到任何输入,因此它将为null,ModelState.IsValid()将为false