1

我有以下代码:

    <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);
    }

基本上,用户名字段调用 IsUsernameAvailable 方法。验证触发并调用该方法,但变量“用户名”每次都有一个空值,因此验证始终为真。有任何想法吗?

编辑:这是用户名字段

    [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();
            }
        }
    }
4

1 回答 1

1

您是否尝试指定前缀:

public ActionResult IsUserNameAvailable([Bind(Prefix="Item1")] string username)
{
    ...
}
于 2012-04-30T21:51:15.347 回答