mvc3范围验证

时间:2013-03-18 05:42:43

标签: javascript jquery asp.net-mvc-3 validation

我的mvc3页面中有三个字段:rate,minrate和maxrate。速率应高于minrate且小于maxrate。如何通过mvc3模型验证实现这一目标?  这是模型:

   [Required(ErrorMessage = "Rate Required")]
    [Range(1, 9999999.999, ErrorMessage = "Invalid Rate")]

     public decimal rate
    {
        get;
        set;
    } 
    /// <summary>
    //min Rate
    /// </summary>
      [Required(ErrorMessage = "Min.Rate Required")]
      [Range(1, 9999999.999, ErrorMessage = "Invalid Rate")]

    public decimal minRate
    {
        get;
        set;
    }
    /// <summary>
    //max Rate
    /// </summary>
      [Required(ErrorMessage = "Max.Rate Required")]
      [Range(1, 9999999.999, ErrorMessage = "Invalid Rate")]

    public decimal maxRate
    {
        get;
        set;
    }

修改

我尝试使用javascript在客户端执行比较。如果我能找到一种动态更改验证消息的方法(例如:在maxrate字段的情况下'需要最大速率'),我可以解决问题。有没有办法做到这一点?

我试过这个:(省略了一些代码)

//...........................
$('#Save').click(function(){

  if (parseFloat(maxrate) < parseFloat(rate)) {

                  $('#maxRateforvalidation').text("Rate must be less than maxrate");
//It works but the styles for the validation message are not applied

                $('#maxRateforvalidation').show();
                 return false;
             }
});

修改

以下是cshtml页面中代码的相关部分:

  @using (Html.BeginForm("Saveaction", "mycontroller", FormMethod.Post))
     {

     ..................
     <dt>Rate</dt>
     <dd>@Html.TextBoxFor(model => model.rate, new { style = "width:28%"}) 
          @Html.ValidationMessageFor(model => model.rate)  
     </dd> 
     <dt>minRate</dt>
     <dd>@Html.TextBoxFor(model => model.minrate, new { style = "width:28%"}) 
          @Html.ValidationMessageFor(model => model.minrate)  
     </dd>
     <dt>maxRate</dt>
     <dd>@Html.TextBoxFor(model => model.maxrate, new { style = "width:28%"}) 
          @Html.ValidationMessageFor(model => model.maxrate)  
     </dd>
   .........................

 <button type="submit" id="Save" >  Save(F2)</button>

}

修改

我检查了运行时生成的代码,对于rate字段,当我把它留空并单击提交按钮时:

<span id="minRateforvalidation" class="field-validation-error" data-valmsg-replace="true" data-valmsg-for="minRate" style="display: inline;">
<span class="" htmlfor="minRate" generated="true">Min.Rate Required</span>
</span>

所以我更新了这样的代码:

if(parseFloat(maxrate)&lt; parseFloat(rate)){

                    $('#maxRateforvalidation').html('<span class="" htmlfor="maxRate"  generated="true" style="visibility: hidden;">Max.Rate must be higher than rate</span>');


                     $('#maxRateforvalidation').addClass("field-validation-error");

                  $('#minRateforvalidation').show();
                  return false;
                }

现在它有效..但只有当我用firebug检查内容并提交!!无法弄清楚原因。有没有更简单的方法来执行此验证,例如在模型中设置范围如下所示:

 [Required(ErrorMessage = "Min.Rate Required")]
       [Range(1, rate, ErrorMessage = "Invalid Rate")]// cant do it.

        public decimal minRate
        {
            get;
            set;
        }

0 个答案:

没有答案