Bha*_*han 5 c# jquery asp.net-mvc-3 remote-validation
这是我的型号代码
public class BlobAppModel
{
[Required(ErrorMessage="Please enter the name of the image")]
[Remote("IsNameAvailable","Home",ErrorMessage="Name Already Exists")]
public string Name { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有
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);
}
Run Code Online (Sandbox Code Playgroud)
在我的数据中我有
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我也添加了脚本
<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>}
Run Code Online (Sandbox Code Playgroud)
但远程验证根本没有触发..我的代码有什么问题吗?
fro*_*ost 12
确保您的验证方法使用[AllowAnonymous]属性修饰(也可能需要[HttpPost]).
[AllowAnonymous]
public JsonResult IsNameAvailable(string Name)
Run Code Online (Sandbox Code Playgroud)
还有一个提示:使用浏览器的开发人员工具(主流浏览器中的F12按钮)来查看Json请求的状态.
您jquery.unobtrusive-ajax.js在视图中缺少该文件,请添加该文件,然后重试。
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
如果没有的话,它是从小部件进入的
nuget链接http://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/
| 归档时间: |
|
| 查看次数: |
12180 次 |
| 最近记录: |