I have the following code:
<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>
And:
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);
}
Basically, the Username field calls the IsUsernameAvailable method. The validation fires and the method is called but the variable “Username” has a null value every time so the validation is always true.
Any ideas?
EDIT:
This is the Username field
[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();
}
}
}
Did you try specifying a prefix: