简体   繁体   English

客户端验证MVC下拉列表

[英]Client side validation mvc dropdown

@using (Html.BeginForm("ForatExcel", "ForatSummary", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.DropDownList("ForatFrom", new SelectList(Model, "ID", "ID", new { onchange = "getAllData()" }))
@Html.DropDownList("ForatTo", new SelectList(Model, "ID", "ID", new { onchange = "getAllData()" }))
<br />
<input type="submit" id="btnForatVersion" value="Go"/> 
}

I need to validate that the "ForatFrom" dropdown value is greater than that of the "ForatTo" value. 我需要验证“ ForatFrom”下拉列表值是否大于“ ForatTo”值。 I guess i can't use model validation, as that would just check that the value of the drop down is a particular number. 我猜我不能使用模型验证,因为那只会检查下拉列表的值是特定数字。 I was thinking maybe jquery validation but not sure what the best option would be? 我在想也许jquery验证,但不确定最好的选择是什么?

Thanks 谢谢

You can and should use model validation . 您可以并且应该使用模型验证 I would implement a validation attribute [LargerThan], something like this: 我将实现一个验证属性[LargerThan],如下所示:

public class LargerThanAttribute: ValidationAttribute, IClientValidatable
{
     private string _listPropertyName { get; set; }

     public LargerThanAttribute(string listPropertyName)
     {
         this._listPropertyName = listPropertyName;
     }

     protected override ValidationResult IsValid(object value, ValidationContext validationContext)
     {
        if(value == null)
            return new ValidationResult("Not a valid value");

        var listProperty = validationContext.ObjectInstance.GetType().GetProperty(_listPropertyName);
        double propValue = Convert.ToDouble(listProperty.GetValue(validationContext.ObjectInstance, null));

        if(propValue <= Convert.ToDouble(value))
            return ValidationResult.Success;

        return new ValidationResult("End value is smaller than start value");
    }
}

Note that this code is not tested but if you write something along this line and put it in a seperate class, you can reuse it when ever you need to do this kind of check. 请注意,此代码未经测试,但是如果您沿这行写一些东西并将其放在单独的类中,则在需要进行此类检查时都可以重用它。 You can now put it on a property in your model 您现在可以将其放在模型的属性中

public double ForatFrom { get; set; }

[LargerThan("ForatFrom")]
public double ForatTo { get; set; }

Now you have the server model validation, and if you like you can now implement jQuery unobtrusive validation. 现在,您可以进行服务器模型验证,并且如果愿意,现在可以实现jQuery非侵入式验证。 In my opinion, if you need validation, you should do it atleast on the server and if you need to do it on the client, then implement it there aswell, but never only rely on client validation. 我认为,如果需要验证,则应至少在服务器上进行验证;如果需要在客户端上进行验证,则也应在该位置上进行验证,但绝不要仅依靠客户机验证。

Here's a good post that you can read that will show you what i just did and also explain how to implement client validation: http://thepursuitofalife.com/asp-net-mvc-3-unobtrusive-javascript-validation-with-custom-validators/ 这是一篇不错的文章,您可以阅读它,向您展示我刚才所做的事情,并说明如何实施客户端验证: http : //thepursuitofalife.com/asp-net-mvc-3-unobtrusive-javascript-validation-with-custom -validators /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM