在Asp.Net MVC中无法触发远程属性验证 [英] Remote Attribute Validation not firing in Asp.Net MVC
本文介绍了在Asp.Net MVC中无法触发远程属性验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的型号代码
public class BlobAppModel
{
[Required(ErrorMessage="Please enter the name of the image")]
[Remote("IsNameAvailable","Home",ErrorMessage="Name Already Exists")]
public string Name { set; get; }
}
在我的控制器中,
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>}
但是远程验证根本没有触发.我的代码是否有问题?
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.
这篇关于在Asp.Net MVC中无法触发远程属性验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文