2010-05-27 87 views
1

我想在我的应用程序的asp.net.net MVC 2.0中做一些验证。我想要有一些很好的客户端验证。 DataAnnotations具有自定义属性(比如CompareTo,StringLenght,MinPasswordLenght(来自Membership.MinimumumpassworkdLenght值),因此在模型端应该大多数时间进行验证。 为此,我尝试使用xval和jquery.validation。 一些特定的事情是:的形式将与阿贾克斯和大多数问题是工作的时候我想验证表单与AJAX。asp .net MVC 2.0验证

这里是链接样本项目http://www.sendspace.com/file/m9gl54

我有两种形式作为对照ValidFormControl1.ascx,ValidFormControl2.ascx

<% using (Ajax.BeginForm("CreateValidForm", "Test", new AjaxOptions { HttpMethod = "Post" })) {%> <div id="validationSummary1"> 
    <%= Html.ValidationSummary(true)%> </div> <fieldset> 
    <legend>Fields</legend> 
    <div class="editor-label"> 
     <%= Html.LabelFor(model => model.Name)%> 
    </div> 
    <div class="editor-field"> 
     <%= Html.TextBoxFor(model => model.Name)%> 
     <%= Html.ValidationMessageFor(model => model.Name)%> 
    </div> 
    <div class="editor-label"> 
     <%= Html.LabelFor(model => model.Email)%> 
    </div> 
    <div class="editor-field"> 
     <%= Html.TextBoxFor(model => model.Email)%> 
     <%= Html.ValidationMessageFor(model => model.Email)%> 
    </div> 
    <div class="editor-label"> 
     <%= Html.LabelFor(model => model.Password)%> 
    </div> 
    <div class="editor-field"> 
     <%= Html.TextBoxFor(model => model.Password)%> 
     <%= Html.ValidationMessageFor(model => model.Password)%> 
    </div> 
    <div class="editor-label"> 
     <%= Html.LabelFor(model => model.ConfirmPassword)%> 
    </div> 
    <div class="editor-field"> 
     <%= Html.TextBoxFor(model => model.ConfirmPassword)%> 
     <%= Html.ValidationMessageFor(model => model.ConfirmPassword)%> 
    </div> 
    <p> 
     <input type="submit" value="Create" /> 
    </p> </fieldset> <% } %> <%= Html.ClientSideValidation<ValidModel>() 
    .UseValidationSummary("validationSummary1", "Please fix the following problems:") %> 

两者看起来相同,区别仅在于验证summaryID(validationSummary1,validationSummary2)。这两个控件呈现在一个页面上:

Form2 
    <%Html.RenderPartial("~/Views/Test/ValidFormControl2.ascx", null); %> 
Form1 

<%Html.RenderPartial("~/Views/Test/ValidFormControl.ascx", null); %> 

验证属性

第一个问题,当我们有相同类型的两个控件来验证它不工作becosue HTML元素通过字段名渲染(所以我们有两个具有相同名称“密码”的元素)。只有第一种形式将由客户端验证。 最糟糕的是,即使我们有不同的类型和他们的字段名称是相同的验证也不会工作(这件事是我需要修复它将愚蠢命名一些独特的验证合适的)。

有没有解决方案?

自定义属性验证

接下来的事情自定义属性验证(当我使用Ajax为正常表单验证工作没有问题所有这些错误都是。):

的CompareTo - 简单比较这是在用于帐户模型的mvc模板中完成的(具有两个属性的类属性将被比较),并且它不在页面上显示。为此,我使用compareRule和我的属性创建了自己的CachingRulesProvider。也许有更简单的方法来做到这一点?

StringLenght具有最小值和最大值,我不会描述我是如何做到的,但是有没有简单的乳清来做到这一点?

验证摘要

当我有两个页面上的两个控制所有汇总验证信息转到了第一控制验证摘要元素,甚至XVAL生成的脚本说,elementID是总结不同。任何一个知道如何修复它?

验证信息

是否有任何选项打开消息在地方其中Html.ValidationMessageFor(型号=> model.ConfirmPassword)。 Becsoue对我来说它没有出现。我想不仅有红色边框,还有总结和近场信息。任何人都知道如何去做?

阿贾克斯提交

任何人都知道如何在不大规模的代码做容易在JavaScript中,通过JavaScript提交。这将用于改变输入提交给href元素(a)。 两者看起来相同,区别只是验证总结ID

+3

这里有很多有效的问题。 SO更适合单个问题。可能需要将这些问题解决并重新张贴以获得答案。 – 2010-05-27 15:05:41

回答

0

首先,我不知道为什么你想要两个控件,有两个相同的字段,一种形式。每个表单都应该有自己的模型,并使用模型作为验证器。

命名空间Sample.Models { [元类型(typeof运算(PersonaValidation))] 公共局部类Person { } 公共类PersonValidation { [必需(的ErrorMessage = “名字必需”)] [ StringLength(20,ErrorMessage =“名字长度不得超过20个字符”)] [DisplayName(“First Name”)] public string Firstname {get;组; }

[Required(ErrorMessage = "Last name Required")] 
    [StringLength(20, ErrorMessage = "Last name must be 20 or less characters in length")] 
    [DisplayName("Last Name")] 
    public string Lastname { get; set; } 

}}

在你的aspx,形式标记前添加下面的脚本文件:

<h2>Validation Sample</h2> 

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script> 
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> 
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script> 
<% Html.EnableClientValidation(); %> 

<% using (Html.BeginForm()) {%> 
    <%= Html.ValidationSummary(true) %> 
     <fieldset> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Firstname) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Firstname) %> 
      <%= Html.ValidationMessageFor(model => model.Firstname) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Lastname) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Lastname) %> 
      <%= Html.ValidationMessageFor(model => model.Lastname) %> 
     </div> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 

<% } %> 

这将工作每次。错误消息将显示在无效字段旁边。

0

关于ajax提交,你可以在你的表单上放一个按钮,然后通过jQuery点击它。这也会导致您的表单被张贴。

您也可以对控制器操作进行ajax调用(单击链接),然后在此处发布表单中必要的数据。