2012-02-29 78 views
1

在我的項目中,我似乎對遠程驗證有一個奇怪的問題。我正在對電子郵件字段進行簡單的驗證檢查,以確保它是唯一的。MVC 3遠程驗證提交時出現jQuery錯誤

我注意到,除非我把光標放到文本框中,然後在提交表單之前刪除它至少觸發驗證一次,否則我將得到一個javascript錯誤。

e[h] is not a function jquery.min.js line 3 

如果我嘗試重新提交表單返回上面的錯誤後,一切正常。這幾乎就像在等待驗證返回之前嘗試提交的表單或其他東西。

我是否需要在提交表單前靜靜地提交遠程驗證請求?

下面是我正在使用的代碼的快照:(我也試過GET而不是POST,但是我得到了相同的結果)。如上所述,代碼工作正常,但表單返回一個jquery錯誤,除非驗證觸發至少一次。

型號:

public class RegisterModel 
    { 
     [Required] 
     [Remote("DoesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name taken.")] 
     [Display(Name = "User name")] 
     public string UserName { get; set; } 

     [Required] 
     [Display(Name = "Firstname")] 
     public string Firstname { get; set; } 

     [Display(Name = "Surname")] 
     public string Surname { get; set; } 

     [Required] 
     [Remote("DoesEmailExist", "Account", HttpMethod = "POST", ErrorMessage = "Email taken.", AdditionalFields = "UserName")] 
     [Display(Name = "Email address")] 
     public string Email { get; set; } 

     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)] 
     [DataType(DataType.Password)] 
     [Display(Name = "Password")] 
     public string Password { get; set; } 

     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)] 
     [DataType(DataType.Password)] 
     [Display(Name = "Confirm password")] 
     public string ConfirmPassword { get; set; } 

     [Display(Name = "Approved?")] 
     public bool IsApproved { get; set; } 
    } 

public class UserRoleModel 
    { 
     [Display(Name = "Assign Roles")] 
     public IEnumerable<RoleViewModel> AllRoles { get; set; } 
     public RegisterModel RegisterUser { get; set; } 
    } 

控制器:

// POST: /Account/DoesEmailExist 
    // passing in username so that I can ignore the same email address for the same user on edit page 

    [HttpPost] 
    public JsonResult DoesEmailExist([Bind(Prefix = "RegisterUser.Email")]string Email, [Bind(Prefix = "RegisterUser.UserName")]string UserName) 
    { 
     var user = Membership.GetUserNameByEmail(Email); 
     if (!String.IsNullOrEmpty(UserName)) 
     { 
      if (user == UserName) 
       return Json(true); 
     } 
     return Json(user == null); 
    } 

查看:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js" type="text/javascript"></script> 

<script type="text/javascript" src="/Content/web/js/jquery.unobtrusive-ajax.min.js"></script> 
<script type="text/javascript" src="/Content/web/js/jquery.validate.min.js"></script> 
<script type="text/javascript" src="/Content/web/js/jquery.validate.unobtrusive.min.js"></script> 
...... 


@using (Html.BeginForm()) 
      { 
       @Html.AntiForgeryToken() 

       <div class="titleh"> 
        <h3>Edit a user account</h3> 
       </div> 
       <div class="body"> 

        @Html.HiddenFor(model => model.RegisterUser.UserName) 
        @Html.Partial("_CreateOrEdit", Model) 

        <div class="st-form-line"> 
         <span class="st-labeltext">@Html.LabelFor(model => model.RegisterUser.IsApproved)</span> 
         @Html.RadioButtonFor(model => model.RegisterUser.IsApproved, true, new { @class = "uniform" }) Active 
         @Html.RadioButtonFor(model => model.RegisterUser.IsApproved, false, new { @class = "uniform" }) Disabled 
        <div class="clear"></div> 
        </div> 

        <div class="button-box"> 
         <input type="submit" name="submit" value="Save" class="st-button"/> 
         @Html.ActionLink("Back to List", "Index", null, new { @class = "st-clear" }) 
        </div>      
       </div> 
      } 

CreateEdit管窺

@model Project.Domain.Entities.UserRoleModel 

<div class="st-form-line"> 
    <span class="st-labeltext">@Html.LabelFor(m => m.RegisterUser.Firstname)</span> 
    @Html.TextBoxFor(m => m.RegisterUser.Firstname, new { @class = "st-forminput", @style = "width:300px" }) 
    @Html.ValidationMessageFor(m => m.RegisterUser.Firstname) 
<div class="clear"></div> 
</div> 

<div class="st-form-line"> 
    <span class="st-labeltext">@Html.LabelFor(m => m.RegisterUser.Surname)</span> 
    @Html.TextBoxFor(m => m.RegisterUser.Surname, new { @class = "st-forminput", @style = "width:300px" }) 
    @Html.ValidationMessageFor(m => m.RegisterUser.Surname) 
<div class="clear"></div> 
</div> 


<div class="st-form-line"> 
    <span class="st-labeltext">@Html.LabelFor(m => m.RegisterUser.Email)</span> 
    @Html.TextBoxFor(m => m.RegisterUser.Email, new { @class = "st-forminput", @style = "width:300px" }) 
    @Html.ValidationMessageFor(m => m.RegisterUser.Email) 
<div class="clear"></div> 
</div> 

謝謝,

豐富

+0

我敢打賭,問題是你有一個名爲輸入「提交」在您的形式。如果你輸入了一個「提交」的名字,那麼你就不能用form.submit提交你的表單。看看下面的Stackoverflow問題/回答更好的解釋:http://stackoverflow.com/questions/3553584/cannot-submit-form-with-submit/3553600#3553600 – 2012-05-25 19:37:43

回答

0

這裏的問題是完全一樣的狗在評論中提到以上。設置表單名稱和ID爲我解決了這個錯誤。

因此,基於上面的代碼,我取代了看法與此BeginForm部分:

@using (Html.BeginForm("Edit", "Account", FormMethod.Post, new { id = "editForm", name = "editForm" }))