远程Attribue验证在Asp.Net MVC不点火 [英] Remote Attribue Validation not firing in Asp.Net MVC

查看:138
本文介绍了远程Attribue验证在Asp.Net MVC不点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的模型code

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

在我的数据我有

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

但远程验证是不​​是all..Is那里与我的code什么问题?烧制

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

推荐答案

请确保您的验证方法都装饰有[使用AllowAnonymous]属性([HttpPost]可能太需要)。

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

[AllowAnonymous]
public JsonResult IsNameAvailable(string Name)

另外一个技巧:使用浏览器的开发者工具(在主流浏览器F12按钮),查看JSON请求的状态

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

这篇关于远程Attribue验证在Asp.Net MVC不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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