1

我有一个模型:

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

    }

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

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

Edit观点:

@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>

这是动作:

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

1 回答 1

4

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

于 2013-01-26T08:17:51.853 回答