我一直在研究MVC4应用程序,我正在使用Remote Data-Annotation来检查UserName是否已存在于DB中。但我的问题是,当我在TextBox中输入第一个字符时,它调用JSON方法,并且对于每个字符插入,它将调用JSON方法,但我看的是,它应该仅在用户完成将TextName输入TextBox时执行JSON
这是我的类,我用Remote DataAnnotation创建了对象
public class Student
{
[Display(Name = "User Name")]
[Required(ErrorMessage = "Please enter {0}")]
[StringLength(100)] //If i give minLength here as 3 then after entering 3 characters it is going to call JSON Method which i don't want so i removed minLength Attribute
[Remote("IsAvailable", "Home", ErrorMessage = "{0} already in use!")]
public string UserName { get; set; }
}
这是我家庭控制器中的JSON方法,我正在检查用户名
public JsonResult IsAvailable(string UserName)
{
bool blnResult = false;
if (UserName != null)
{
blnResult = !objDBContext.Student.Any(X => X.UserName == UserName);
}
return Json(blnResult, JsonRequestBehavior.AllowGet);
}
我的视图很容易输入用户名
<div class="form-group col-lg-4">
@Html.LabelFor(model => model.UserName, new { @class = "control-label" })
<div>
@Html.TextBoxFor(model => model.UserName, new { @class = "form-control", maxlength = "100" })
@Html.ValidationMessageFor(model => model.UserName, "*")
</div>
</div>
感谢大家提供有价值的反馈!!