MVC没有为'XXX @YYY'样式的电子邮件地址抛出验证错误,但仍然导致无效的模型状态

时间:2015-09-06 22:30:06

标签: asp.net-mvc validation

由于某种原因,在以下模式“xxx @ yyy”中形成的电子邮件字段中输入没有得到验证错误,基本上缺少.domain部分。虽然只是没有触发验证消息,但它会导致模型状态错误。

这是我的ViewModel:

public class SubscriptionForm
{
    [Required, EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }
}

继承我的简单行动:

[HttpPost, ValidateAntiForgeryToken]
public ActionResult Subscribe(SubscriptionForm form)
{
    if(!ModelState.IsValid)
        return Json(new {OK= false});

    var sub = new Subscription();
    sub.Email = form.Email;
    Database.Session.Save(sub);
    return Json(new {OK = true});
}

继承人观点:

@using (Html.BeginForm("subscribe", "subscriptions", FormMethod.Post, new {@class = "form-inline", id = "subscribe-form"}))
{
    @Html.AntiForgeryToken()
    ....
    @Html.TextBoxFor(x => x.Email, new {@class = "form-control", type = "email", placeholder = "your@email.here"})
    <input type="submit" id="subscribe-btn" value="Subscribe Now!" class="btn btn-large btn-success"/>
    @Html.ValidationMessageFor(m => m.Email)
}

一封空白的电子邮件会抛出该消息,因此其他任何无效的电子邮件输入都会显示,例如'abcd''abcd @ .c'等,它唯一的xxx @ yyy似乎有问题。

任何人都知道这是什么造成的?我一直在谷歌搜索过去的一小时无济于事。

修改

继承了Ajax提交的JS     $(document).ready(function(){

$('#subscribe-form').on("submit", function (e) {

    e.preventDefault();

    var $form = $(this);

    console.log($form);

    var $inputs = $form.find("input, select, button, textarea");
    var $url = $form.attr("action");
    var serializedData = $form.serialize();

    $inputs.prop("disabled", true);

    var request = $.ajax({
        url: $url,
        type: "post",
        data: serializedData
    });

    request.done(function (response, textStatus, jqXHR) {
        console.log(response);
    });

    request.fail(function (jqXHR, textStatus, errorThrown) {
        // Log the error to the console
        console.error(
            "The following error occurred: " +
            textStatus, errorThrown
        );
    });

    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

    // Prevent default posting of form

});

0 个答案:

没有答案