1

I have a model:

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

    }

When user click Edit button I show the dialog:

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

The Edit 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")

But when I click sumbit button with empty CarNumber field I don't get any errors. Where is a error?

The needed js library are loaded:

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

This part of generated 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>

This is actions:

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
  • Does the form completely submit or it just doesn't show the errors? Commented Jan 26, 2013 at 7:37
  • Frankly I can't find anything wrong with your code! In the controller when the Edit action receives the model, are you checking ModelState.IsValid? If so is it true or false? Is it possible to show us the Edit action in the controller? Commented Jan 26, 2013 at 8:06
  • @BahmanNikkhahan: I added the code of actions Commented Jan 26, 2013 at 8:15
  • Did you check my suggestion bellow? Commented Jan 26, 2013 at 8:39

1 Answer 1

4

I think adding [ValidateAntiForgeryToken] before your action(in controller) will solve your problem

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.