开发者

jQuery forms server-side validation messages not displayed

I'm using the current version of ASP.net MVC to display a form. Validation is taken care by the included jQuery Validate. The client-side validation is working, however, the server-side validation is not. Instead, form values are sent back to the server for validation, but the server's answer is being ignored by the JS validation code.

The (simplified) form looks like this:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Invite", "Admin"))
{        
    @Html.ValidationSummary(true)
    <ul class="griditem-list">
        <li>
            <div class="editor-label">@Html.LabelFor(m => m.UserEmail)</div>
            <div class="editor-field">@Html.EditorFor(m => m.UserEmail)</div>
            @Html.ValidationMessageFor(m => m.UserEmail)
        </li>
    </ul>       
    <div class="actions">
        <input class="button" type="submit" value="Add" />
    </div>
}

The client-side javascript correctly checks for invalid email addresses and sends the email adress back to the server for further validation. The server answer gets posted back correctly (here: "This email address is already taken"):

jQuery forms server-side validation messages not displayed

The value is not displayed in any way, however, neither near the editor-field nor somewhere else. What could be the reason for this?

Edit:

This is the model that I'm using:

public class InvitationUser
{
    [Required开发者_如何学C(ErrorMessage = "{0} darf nicht leer sein.")]
    [Email(ErrorMessage = "Die Emailadresse ist ungültig")]
    [Remote("EmployeeExists", "Admin", ErrorMessage = "Der angegebene User wurde bereits eingeladen.")]
    [Display(Name="E-Mail")]
    public string UserEmail { get; set; }

    [Required(ErrorMessage = "{0} darf nicht leer sein.")]
    [Display(Name = "Vorname")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "{0} darf nicht leer sein.")]
    [Display(Name = "Nachname")]
    public string LastName { get; set; }

    public IEnumerable<AdminUserRight> Rights { get; set; }
}

Here is EmployeeExists, which posts back the correct result of "Email address already exists":

    public JsonResult EmployeeExists(string UserEmail)
    {
        Guid companyId = AzManPrincipal.Current.Employee.BrokerCompany.ID;

        var container = new ModelContainer();
        if (container.BrokerEmployeeSet.FirstOrDefault(m => m.Email == UserEmail && m.BrokerCompany.ID == companyId) == null &&
            container.BrokerInvitationSet.FirstOrDefault(m => m.Email == UserEmail) == null)
            return Json(true, JsonRequestBehavior.AllowGet);

        string errorMessage = String.Format(CultureInfo.InvariantCulture, "{0} ist bereits registriert.", UserEmail);
        return Json(errorMessage, JsonRequestBehavior.AllowGet);
    }

This is the Index controller which renders the form:

    public ActionResult Index()
    {
        InvitationUser newUser = AdminUserService.CreateInvitationUser();
        return View(newUser);
    }


Updating Jquery to 1.6.1 and Jquery.validation to 1.8.1 worked for me!


If your using the following attribute:

[DataType(DataType.EmailAddress)]

this does not actually validate an email address. There is an email address validator in MVC 3 Futures. If you want to just implement a new type you can look at the following forum post to see how its done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜