jquery - ASP.NET MVC 4 : client validation not work

标签 jquery asp.net-mvc validation unobtrusive-validation

我有一个模型:

public class UserInfoViewModel
    {
        public int Id { get; set; }

        [LocalizedDisplayName("CarNumber", NameResourceType = typeof(CabinetViewModelsStrings))]
        [Required]
        public string CarNumber { get; set; }  

        [LocalizedDisplayName("CarMark", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string CarMark { get; set; }

        [LocalizedDisplayName("CarYear", NameResourceType = typeof(CabinetViewModelsStrings))]
        [IntLengthAttribute(4, ErrorMessage = "Wrong Year")]
        public int CarYear { get; set; }

        [LocalizedDisplayName("Email", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string Email { get; set; }

        [LocalizedDisplayName("PhoneNumber", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string PhoneNumber { get; set; }

        [LocalizedDisplayName("FullName", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string FullName { get; set; }

        [LocalizedDisplayName("FirstName", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string FirstName { get; set; }

        [LocalizedDisplayName("LastName", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string LastName { get; set; }

        [LocalizedDisplayName("MiddleName", NameResourceType = typeof(CabinetViewModelsStrings))]
        public string MiddleName { get; set; }       

    }

当用户单击编辑按钮时,我显示对话框:

$(document).on('click', '#edit_info', function() {
        var dialog = createDialog();
        var editUrl = '/cabinet/edit';
        dialog.load(editUrl, function(content) {
            dialog.dialog({
                close: function(event, ui) {
                    dialog.remove();
                },
                modal: true,
                width: 460,
                resizable: false
            });
        });
    });

编辑 View :

@using (Ajax.BeginForm("Edit", "Cabinet", new AjaxOptions { HttpMethod = "POST" }, new { Id = "update_user_info" })) 
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>@CabinetViewsStrings.FormEditInfo</legend>
        @Html.HiddenFor(m=>m.Id)
        <ol>
            <li>
                @Html.LabelFor(m => m.FirstName)
                @Html.TextBoxFor(m => m.FirstName)
            </li>
            <li>
                @Html.LabelFor(m => m.MiddleName)
                @Html.TextBoxFor(m => m.MiddleName)
            </li>
            <li>
                @Html.LabelFor(m => m.LastName)
                @Html.TextBoxFor(m => m.LastName)
            </li>
            <li>
                @Html.LabelFor(m => m.Email)
                @Html.TextBoxFor(m => m.Email)
            </li>
            <li>
                @Html.LabelFor(m => m.PhoneNumber)
                @Html.TextBoxFor(m => m.PhoneNumber)
            </li>
            <li>
                @Html.LabelFor(m => m.CarNumber)
                @Html.TextBoxFor(m => m.CarNumber)
                @Html.ValidationMessageFor(model => model.CarNumber)
            </li>                
            <li>
                @Html.LabelFor(m => m.CarMark)
                @Html.TextBoxFor(m => m.CarMark)
            </li>                
            <li>
                @Html.LabelFor(m => m.CarYear)
                @Html.TextBoxFor(m => m.CarYear)
                @Html.ValidationMessageFor(model => model.CarYear)
            </li>      
        </ol>
        <input id="update_user_info" type="submit" value="@CabinetViewsStrings.ButtonSaveEditInfo" />
    </fieldset>
}

@Scripts.Render("~/bundles/jqueryval")

但是当我单击带有空 CarNumber 字段的 sumbit 按钮时,我没有收到任何错误。 哪里有错误?

所需的js库已加载:

GET jquery.unobtrusive-ajax.js?_=1359182513969
GET jquery.validate.js?_=1359182514123
GET jquery.validate.unobtrusive.js?_=1359182514379

这部分生成的html:

<li>
     <label for="CarNumber">Car Number</label>
     <input type="text" value="B650LZN" name="CarNumber" id="CarNumber" data-val-required="The Car Number field is required." data-val="true">
</li>

这是 Action :

public ActionResult Edit()
{
   var model = _viewModelCreator.Create<UserInfoViewModel, User>(PcpSession.CurrentUser);
   return PartialView(model);
 }

 [HttpPost]
 public ActionResult Edit(UserInfoViewModel userInfo)
 {
    if (!ModelState.IsValid)
    {
       return View(userInfo);
    }

    var user = _entityCreator.Create<User, UserInfoViewModel>(userInfo);
    _userService.UpdateUser(user);
    PcpSession.CurrentUser = user;

    return Json(new { });
 }

最佳答案

我认为在您的操作(在 Controller 中)之前添加 [ValidateAntiForgeryToken] 将解决您的问题

关于jquery - ASP.NET MVC 4 : client validation not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534631/

相关文章:

javascript - 根据参数更新数组中的值

javascript - 在 Symfony2 中通过 ajax 提交表单

php - 使用 PHP 确保字符串只包含数字、字母和下划线?

javascript - Vue.js - 当用户单击组件之外的位置时显示表单错误

鼠标悬停时 JavaScript SlideDown 不起作用

jquery - 绝对元素 jQuery 居中问题

asp.net-mvc - Visual Studio 不在我的 Asp.net MVC View 中显示 Linq 扩展

asp.net-mvc - 用于 asp.net mvc 的回形针

asp.net-mvc - Azure 代理服务和 SqlTableDependency

javascript - Kendo UI 验证 - 禁用/启用