English 中文(简体)
进行客户方验证的最佳方式是检查马克斯价值高于马诺克在我方的印本内的价值。
原标题:what is the best way to implement a client side validation to check that the Max value is greater than the Min value inside my asp.net MVC

i have an object which contains Max value and Min value,, so i want inside all the edit and create views to display a client side validation message incase the user insert the Min value greater then the Min value. BR

问题回答

你可能希望客户和服务器方面的验证。 落实你自己的成果也许是好的。 我也做了类似的事情,我希望确保一个领域与另一个领域具有同等价值。 不过,我并没有从零开始。 我使用了微软Simon J Ince的一些代码。 他有here。 基本来说,他有一个有条件补偿的基础(根据另一个领域的价值验证)。 我继承了他有条件的AttributeBase,并实施了IClientValidatable接口(这就是,你如何把java的手稿送到附属地的浏览器)。

/// <summary>
/// A validator for ensuring that the value of this field does NOT equal the value of another field.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class NotEqual:ConditionalAttributeBase, IClientValidatable
{
    public string DependentProperty { get; set; }

    /// <summary>
    /// Returns client validation rules for NotEqual
    /// </summary>
    /// <param name="metadata">The model metadata.</param>
    /// <param name="context">The controller context.</param>
    /// <returns>
    /// The client validation rules for NotEqual.
    /// </returns>
    IEnumerable<ModelClientValidationRule> IClientValidatable.GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
                       {
                           ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
                           ValidationType = "notequal",
                       };
        string depProp = BuildDependentPropertyId(metadata, context as ViewContext);
        rule.ValidationParameters.Add("dependentproperty", depProp);

        yield return rule;
    }

    /// <summary>
    /// Builds the dependent property id.
    /// </summary>
    /// <param name="metadata">The metadata.</param>
    /// <param name="viewContext">The view context.</param>
    /// <returns></returns>
    protected string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
    {
        return QualifyFieldId(metadata, DependentProperty, viewContext);
    }

    /// <summary>
    /// Validates that the value does not equal the value of the dependent value if the dependent value is not null
    /// </summary>
    /// <param name="value">The value to validate.</param>
    /// <param name="validationContext">The context information about the validation operation.</param>
    /// <returns>
    /// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
    /// </returns>
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // check if the current value matches the target value
        if (value != null && GetDependentFieldValue(DependentProperty, validationContext).ToString() == value.ToString())
        {

                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }

        return ValidationResult.Success;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NotEqual"/> class.
    /// </summary>
    /// <param name="dependentProperty">The dependent property.</param>
    public NotEqual(string dependentProperty)
    {
        DependentProperty = dependentProperty;
    }

}$

随后,我打着 j子:

    (function ($) {
        $.validator.addMethod( notequal ,
            function (value, element, parameters) {
                var id =  #  + parameters[ dependentproperty ];
                var depControl = $(id);
                var control = $(element);
                if (control.val() === depControl.val())
                    return "";
                return true;
            }
        );

    $.validator.unobtrusive.adapters.add(
         notequal ,
        [ dependentproperty ],
        function (options) {
            options.rules[ notequal ] = {
                dependentproperty: options.params[ dependentproperty ]
            };
            options.messages[ notequal ] = options.message;
        }
    );

        $.validator.addMethod( notequaltwo ,
        function (value, element, parameters) {
            var id =  #  + parameters[ dependentproperty ];
            var depControl = $(id);
            var control = $(element);
            if (control.val() === depControl.val())
                return "";
            return true;
        }
    );

        $.validator.unobtrusive.adapters.add(
         notequaltwo ,
        [ dependentproperty ],
        function (options) {
            options.rules[ notequaltwo ] = {
                dependentproperty: options.params[ dependentproperty ]
            };
            options.messages[ notequaltwo ] = options.message;
        }
    );
})(jQuery);$

希望你们能够看到你如何调整我的准则,以做你想要做的事情,但基本上,你们必须改变这些类型(如果这些类型不改变,就没有做什么,也没做你应该做的其他人)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签