我的项目远程验证似乎有一个奇怪的问题。我正在对电子邮件字段进行简单的验证检查,以确保它是唯一的。
我注意到,除非我将光标放入文本框然后将其删除以在提交表单之前至少触发一次验证,否则会出现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 Partial View
@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 :(得分:0)
这里的问题正如The Dog在上面的评论中提到的那样。设置表单名称和ID为我修复了这个错误。
基于上面的代码,我用这个替换了视图中的BeginForm部分:
@using (Html.BeginForm("Edit", "Account", FormMethod.Post, new { id = "editForm", name = "editForm" }))