Jean-Francois
Jean-Francois

Reputation: 1949

How to remote validation with Asp.Net MVC 2

I Create this class.

  public class UniqueFileNumber : ValidationAttribute
    {

        private string _LocationFile;

        public override string FormatErrorMessage(string str)
        {
            return ViewRes.ValidationString.Loc_FileNumberExist;
        }

        public override bool IsValid(object value)
        {
            DBEntities _db = EntityFactory.GetEntity();
            string strName = Convert.ToString(value);
            return !_db.Locations.Any(p => p.LocationFile == strName);

        }
    }

and add this attribute to my entity Like that.

[UniqueFileNumber]
public object FileNumber{ get; set; }

The validation work only on the PostBack (Refresh).

It would be fine if it's work on client side too. In my client side , I add that line

<% Html.EnableClientValidation(); %>

What's the problem here.

thanks.

Upvotes: 0

Views: 557

Answers (2)

pocheptsov
pocheptsov

Reputation: 1956

One more Remote Validation ASP.NET MVC 2 based on Brad Wilson idea, but used jQuery ajax calls.

Upvotes: 0

moi_meme
moi_meme

Reputation: 9318

You have to give it the javascript fonction to use for validation...

check out this post from Phil Haacked ASP.NET MVC 2 Custom Validation

A better example for what you have to do since you still need to go on the server, check this post from Brad Wilson Remote Validation with ASP.NET MVC 2

Upvotes: 1

Related Questions