English 中文(简体)
客户随行的习俗
原标题:Client side custom validaton with attribute doesn t work

我建立了习俗验证属性。

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class ValidateDublicateNameAttribute : ValidationAttribute, IClientValidatable
    {
        private const string _defaultErrorMessage = "Library with title {0} is already exist";
        private UnitOfWork unit = new UnitOfWork();

        public ValidateDublicateNameAttribute()
            : base(_defaultErrorMessage)
        {
        }
        public override s

tring FormatErrorMessage(string name) { return String.Format(ErrorMessageString, name); }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string valueAsString = value as string;
        ValidationResult result = ValidationResult.Success;
        if (String.IsNullOrEmpty(valueAsString))
        {
            if (unit.ResourceSrvc.GetLibraryByTitle(valueAsString) != null)
            {
                result = new ValidationResult(String.Format(_defaultErrorMessage,value));
            }
        }
        else
        {
            result = new ValidationResult("Title cant be empty or null");
        }
        return result;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage,
            ValidationType = "dublicatename",
        };
        yield return rule;
    }
}

并且利用模型对我的模型进行校正

public class ResourceLibraryModel
{
    public Guid LibraryId { get; set; }
    [Required]
    [ValidateDublicateName(ErrorMessage="title cant dublicate")]
    public string Title { get; set; }
}

在客户方面,我

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $.validator.addMethod("dublicatename", function (value, element, params) {
            alert("test");
        });

    });
</script>

我没有任何验证参数,也没有打字笔字。

服务器侧验证完全可行,但客户方不可行。

任何想法?

这一观点

     @using (Ajax.BeginForm("CreateLibrary", "Resource", new AjaxOptions { OnSuccess = "RequestSucceeded"}))
        {
            @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
                <div>
                    @*<div style="margin-bottom:15px">
                        <label><b>Library information:</b></label>
                    </div>*@

                    <div class="editor-field">
                        @Html.TextBoxFor(m => m.Title, new { @class = "logon-field" })
                        @Html.ValidationMessageFor(m => m.Title)
                    </div>
                    <div class="editor-label">
                        @Html.LabelFor(m => m.Title)
                    </div>
                 </div>
}
最佳回答

我认为,问题是<条码>jquery.validate.min.js。 必须在您的javascript 之后装载......

问题回答

仅是一位学者,但你还是能够使客户在你的网站上得到验证。

 <appSettings>
  <add key="ClientValidationEnabled" value="true"/> 
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
 </appSettings>

If so follow the Enabling Client-Side Validation section in this link: http://www.asp.net/mvc/tutorials/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript





相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签