我有以下代码:
<td colspan="5" align="left" valign="top">
<div style="position:absolute;" >@Html.TextBoxFor(a => a.Item1.Username, new { @class = "textboxLarge" })
@Html.ValidationMessageFor(a => a.Item1.Username)</div>
</td>
和
public JsonResult IsUserNameAvailable(string Username)
{
var users = _userService.GetAll();
var userNames = (from u in users
select u.Username).ToList();
if (userNames.Contains(Username))
{
var referrer = Request.UrlReferrer.AbsolutePath;
List<string> pagesToValidate = new List<string>() { "Registro" };
var jsonRequestBehavior = JsonRequestBehavior.DenyGet;
foreach (string page in pagesToValidate)
{
if (referrer.Contains(page))
jsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
return Json("El nombre de usuario ya existe.", jsonRequestBehavior);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
基本上,Username字段调用IsUsernameAvailable方法。验证触发并调用方法,但变量“Username”每次都有一个空值,因此验证始终为true。 有什么想法吗?
编辑: 这是用户名字段
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Username", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
[Required(ErrorMessage="El nombre de usuario es requerido.")]
[Remote("IsUserNameAvailable", "Validation")]
public string Username
{
get
{
return this._Username;
}
set
{
if ((this._Username != value))
{
this.OnUsernameChanging(value);
this.SendPropertyChanging();
this._Username = value;
this.SendPropertyChanged("Username");
this.OnUsernameChanged();
}
}
}
答案 0 :(得分:1)
您是否尝试指定前缀:
public ActionResult IsUserNameAvailable([Bind(Prefix="Item1")] string username)
{
...
}