客户端验证MVC下拉列表 [英] Client side validation mvc dropdown

查看:80
本文介绍了客户端验证MVC下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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"/> 
}

我需要验证"ForatFrom"下拉列表值是否大于"ForatTo"值.我猜我不能使用模型验证,因为那只会检查下拉列表的值是否为特定数字.我在想也许是jquery验证,但不确定最好的选择是什么?

I need to validate that the "ForatFrom" dropdown value is greater than that of the "ForatTo" value. 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?

谢谢

推荐答案

您可以并且应该使用模型验证.我将实现一个验证属性[LargerThan],如下所示:

You can and should use model validation. I would implement a validation attribute [LargerThan], something like this:

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; }

现在,您已经进行了服务器模型验证,并且如果愿意的话,现在可以实现jQuery非侵入式验证.我认为,如果需要验证,则应至少在服务器上进行验证,如果需要在客户端上进行验证,则也可以在服务器上进行验证,但绝不要仅依靠客户机验证.

Now you have the server model validation, and if you like you can now implement jQuery unobtrusive validation. 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/

这篇关于客户端验证MVC下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆