1

I have problems getting the validation summary to show in an ajax form.

Web.Config

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

Partial View:

@Html.ValidationSummary()

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "file" }))

Controller:

    [HttpPost]
    public PartialViewResult Move(List<MoveFileResult> moveFileResult)
    {
        if (ModelState.IsValid)
        {    
        }

        // ModelState.AddModelError("err", "eerrr");
  ....

Rendered Html:

<input checked="checked" id="Replace3665" 
     name="MoveFileResult[0].MoveFileAction" 
     type="radio" value="Replace"> Overwrite
<input id="Rename3665" 
     name="MoveFileResult[0].MoveFileAction" 
     type="radio" value="Rename">
Rename to:
<input id="MoveFileResult_0__NewFileName" 
     name="MoveFileResult[0].NewFileName" 
     type="text" value="">
<span class="field-validation-valid" 
   data-valmsg-for="MoveFileResult[0].NewFileName" 
   data-valmsg-replace="true"></span>

Scripts: (UPDATED)

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

They are created like this:

@Html.TextBox(String.Format("MoveFileResult[{0}].NewFileName", i), item.NewFileName)
@Html.ValidationMessage(String.Format("MoveFileResult[{0}].NewFileName", i), item.NewFileName)

ViewModel:

namespace MyProj.MVC.Areas.Admin.ViewModels
{
    public class MoveFilesViewModel
    {

        public List<MoveFileResult> MoveFileResult { get; set; }
    }

    public class MoveFileResult
    {
        [Required]
        public int FileID { get; set; }

        [Required]
        public MoveFileAction MoveFileAction { get; set; }

        //[RequiredIf("MoveFileAction", 2)]
        [Required]
        public string NewFileName { get; set; }    
    }

    public enum MoveFileAction : byte
    {
        Move = 0,
        Replace = 1,
        Rename = 2
    }
}
  1. If I run the ModelState.AddModelError("err", "eerrr"); It will show summary
  2. If I debug, ModelState contains errormessages for each "NewFileName"

Debug

Somehow the errormessage doesnt exist in my custom Html-list.

How do I add it to this custom listning of radiobuttons + textbox?

1
  • Where is the code for your MoveFileResult? Commented May 24, 2013 at 8:02

1 Answer 1

2

you should also add

jquery.validate.js

in the order

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

This is a very nice reference on Unobtrusive Client Validation in ASP.NET MVC 3.

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

2 Comments

I added the file in the that order, but still nothing happens and the textbox doesnt get errormessage
I think you should use html.textboxfor instead of html.textbox. same in the case of validationMessage.

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.