7

Here is my model code

public class BlobAppModel
{
   [Required(ErrorMessage="Please enter the name of the image")]
   [Remote("IsNameAvailable","Home",ErrorMessage="Name Already Exists")]
   public string Name { set; get; }           
}

And in my controller I have

public JsonResult IsNameAvailable(string Name)
{
   bool xx= BlobManager.IsNameAvailable(Name);
   if (!xx)
   {
      return Json("The name already exists", JsonRequestBehavior.AllowGet);
   }
   return Json(true, JsonRequestBehavior.AllowGet);
}

And in my data I have

public static bool IsNameAvailable(string Name)
{
   var test = "";
   using (var x = new BlobTestAppDBEntities())
   {
       try
       {
            test=x.BlobApps.Where(m => m.Name == Name).FirstOrDefault().Uri;
            if (test != null)
               return false;
            else
               return true;
       }
       catch (Exception)
       {
          return true;
       }
   }
}

In my view I have added the scripts too

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {

    <td> @Html.Label("Name:") 
                 @Html.TextBoxFor(m => m.Name)
                 @Html.ValidationMessageFor(m=>m.Name)</td>}

But remote validation is not firing at all..Is there any problem with my code?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Bharat Bhushan
  • 1,175
  • 3
  • 15
  • 24

4 Answers4

13

Make sure that your validation method is decorated with [AllowAnonymous] attribute ([HttpPost] may be required too).

[AllowAnonymous]
public JsonResult IsNameAvailable(string Name)

Also a tip: use browser's developer tools (F12 button in major browsers) to see the status of Json request.

frost
  • 735
  • 1
  • 8
  • 19
  • thanks, its work for me, i just add [AllowAnonymous] attribute at top of the action it was missing. – adnan May 25 '16 at 05:20
  • Thanks a lot. it worked for me. I forgot that I added [Authorize] on controller level and I am accessing this for [Remote] on registration page which should be accessed anonymously. Saved my lot of time. Thanks again :) – Jagdish Chopde Jan 27 '19 at 13:44
5

you are missing the jquery.unobtrusive-ajax.js file in your view please add that and try it again.

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

if you don't have it get in from the nuget

nuget link http://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/

Anto Subash
  • 2,755
  • 2
  • 21
  • 28
  • 1
    are you seeing any errors on the console? and also please remove the min files and try it with the actual files. it is easy to debug. – Anto Subash Oct 30 '13 at 12:30
  • No I am not getting any errors ..I have even added the actual files now..But still there is no change – Bharat Bhushan Oct 30 '13 at 12:33
  • try adding the `jquery.unobtrusive-ajax.js` before `jquery.validate.js` clear the cache before trying again. – Anto Subash Oct 30 '13 at 12:38
  • Done even doing that but still it doesn't fire :( – Bharat Bhushan Oct 30 '13 at 12:44
  • When you make changes to the javascript included sometimes I find the cache will need clearing in my web browser. Also, use the developer tools of your browser (I use chrome) and look at the url generated on the remote validation attribute on the input element you assigned it to. Make sure that relative address is correct. – Csharpfunbag Jul 27 '14 at 00:28
  • Thank you for this answer. It just solved my problem. – Sorangwala Abbasali Sep 30 '16 at 09:52
3

I know this is a little late, If you are using Razor syntax, I found that in addition to everything you did you also need:

@Scripts.Render("~/bundles/jqueryval")
Chris
  • 278
  • 2
  • 13
2

These settings need to go in web.config too:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
djm
  • 19
  • 1
  • 4