ASP MVC - 表单输入类型和模型绑定问题

时间:2017-05-27 21:13:09

标签: asp.net-mvc html5

我正在玩服务器端表单验证,模型绑定和ModelState,我注意到以下内容:

  • 如果我输入的类型为“email”,我会插入无效的电子邮件 地址(例如'hello'),模型绑定不起作用 - 值 显示为“null”。
  • 但是,如果我插入有效的电子邮件地址, 一切顺利。

有人可以向我解释为什么会这样吗? 提前致谢!

形式:

<form name="contactForm" id="contactF" ng-controller="Contacts"  ng-submit="contactForm.$invalid ? sendMessage() : return;" novalidate>   
    <input type="email" name="email" ng-model="model.EmailAddress" required placeholder="Email"/>
    <div ng-messages="contactForm.email.$error" ng-if="contactForm.email.$touched || contactForm.$submitted">
        <div ng-messages-include="/Helpers/error-messages.html"></div>
    </div>

    <textarea name="message" ng-model="model.message" required placeholder="Escreva a sua mensagem aqui."></textarea>
    <div ng-messages="contactForm.message.$error" ng-if="contactForm.message.$touched || contactForm.$submitted">
        <div ng-messages-include="/Helpers/error-messages.html"></div>
    </div>
    <br />
    <input type="submit" value="Enviar"/>
</form>

型号:

public class HomeContactVM 
{
    [Required(ErrorMessage = "Error message here")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Error msg here.")]
    [EmailAddress(ErrorMessage = "Please use a valid email address.")]
    public string EmailAddress { get; set; }

    [Required(ErrorMessage = "Error msg here.")]
    public string Message { get; set; }
}

行动方法:

[HttpPost]
public JsonResult Contact(HomeContactVM model) 
{
    string message;
    if (ModelState.IsValid) 
    {
        using (SmtpClient client = new SmtpClient()) 
        {
            MailMessage msg = new MailMessage()
            {
                Body = model.Message,
                BodyEncoding = Encoding.UTF8,
                Subject = "New message from " + model.EmailAddress,
                From = new MailAddress(model.EmailAddress),
                Sender = new MailAddress("xxx@gmail.com", "xx")
            };

            int retries = 5;
            bool retry = true;
            while (retry) 
            {
                try 
                {
                    client.Send(msg);
                    message = "Your message was sent.";
                    return Json(message, JsonRequestBehavior.AllowGet);
                }
                catch (Exception) 
                {
                    if (retries > 0) {
                                retries--;
                    }
                    else {
                        retry = false;
                        message = "Something went wrong. Please try again later.";
                        return Json(message, JsonRequestBehavior.AllowGet);
                    }
                }
            }
        }
    }
    message = "Your model is not valid.";
    return Json( message, JsonRequestBehavior.AllowGet);
}

1 个答案:

答案 0 :(得分:0)

所以,如果有人遇到同样的问题,这就是我发现的:问题与ASP无关,这是由于Angular的默认行为。 如果输入验证失败,Angular会将其值设置为undefined,并且不会将其传递给模型,因此它显示为null。可能的解决方法是设置ng-model-options =“{allowInvalid:true}”

您可以在此处找到有关此行为的更多信息:Angular.js - ngModel value is undefined when ng-pattern is set in directiveGet "raw" value from invalid input field

希望这有帮助!