這裏是我的模型代碼遠程Attribue驗證在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;
}
}
}
在我看來,我也添加了腳本
<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>}
但遠程驗證不會解僱。是否有任何問題與我的代碼?
類似的問題是另外一個頁面: HTTP://計算器.com/a/37366502/4146766 –