1

我有一个表单,我成功地使用带有 [remote] 注释的不显眼验证。我在模型中有带有 [required] 注释的表单中的其他字段,但我不希望对这些字段进行客户端验证。

我只想要 [required] 字段的服务器端验证

我还没有找到解决方案,我想知道它是否容易实现?

编辑 :

我的代码的一部分

我的模型的一部分:

我想第一个字段:“电子邮件”使用不显眼的验证,第二个字段:“PasswordHash”仅使用服务器端验证,即使它有 [required] 注释。

最后,我不希望我的所有表单字段都出现 AJAX 错误消息。

[Required(ErrorMessageResourceType = typeof(Project.Resources.Models.Accounts.User), ErrorMessageResourceName = "EMAIL_REQUIRED")]
[Display(Name = "EMAIL_DISPLAY", ResourceType = typeof(Project.Resources.Models.Accounts.User))]
[Remote("EmailExists", "User", "An account with this email address already exists.")]
public string Email { get; set; }

[Required(ErrorMessageResourceType = typeof(Project.Resources.Models.Accounts.User), ErrorMessageResourceName = "PASSWORDHASH_REQUIRED")]
[DataType(DataType.Password)]
[Display(Name = "PASSWORDHASH_DISPLAY", ResourceType = typeof(Project.Resources.Models.Accounts.User))]
public string PasswordHash { get; set; }

控制器服务器端验证中的部分操作:

    [HttpPost]
    public ActionResult Register(User user)
    {

        if (ModelState.IsValid )
        {   

            DataContext.User.Add(user);
            DataContext.SaveChanges();

            return RedirectToAction("Index");
        }

        return View(user);
    }

Ajax 验证:

public JsonResult EmailExists(string email)
{
    User user = DataContext.User.SingleOrDefault(x => x.Email == email);

    return user == null ?
        Json(true, JsonRequestBehavior.AllowGet) :
        Json(
            string.Format("an account for address {0} already exists.",
            email), JsonRequestBehavior.AllowGet);
}

部分观点:

 @using (Html.BeginForm())
 {
    @Html.ValidationSummary(true)


    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PasswordHash)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PasswordHash)
        @Html.ValidationMessageFor(model => model.PasswordHash)
    </div>

    <p>
        <input type="submit" name="op" id="edit-submit" value="@Project.Resources.Views.User.Register.SUBMIT_FORM" class="form-submit art-button" />
    </p>
 }

当我单击 sumbit 按钮时,我想要一个服务器端验证。

对于一些特定的领域,比如电子邮件,我想要一个 Ajax 验证。

4

1 回答 1

0

可能有更好的方法,但实现此目的的一种方法是执行以下操作

@using (Html.BeginForm("Register", "Home", FormMethod.Post, new { id = "registerForm" }))
 {
    @Html.ValidationSummary(true)


    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PasswordHash)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PasswordHash)
        @Html.ValidationMessageFor(model => model.PasswordHash)
    </div>

    <p>
        <input type="submit" name="op" id="edit-submit" value="@Project.Resources.Views.User.Register.SUBMIT_FORM" class="form-submit art-button" />
    </p>
 }

<script type="text/javascript">

    // add the rules for the fields that you want client-side validation on
    // leave out the fields that you dont want client-side validation. they will be validated 
    // on server side.
    $("#registerForm").validate({
        rules: {
            Email: { required: true }
        },
        messages: {
            Email: { required : "Email is required" }
        }
    });
</script>
于 2012-11-10T22:17:16.587 回答