Steve Sanderson's BeginCollectionItem not working in all cases... potential solution?
Asked Answered
D

1

7

I'm working with Steve Sanderson's BeginCollectionItem utility to render a list of objects to be edited in MVC3, and it works great when you're rendering an entire collection from an iterator. My problem is coming when I'm trying to just add one new item to the collection, and return the html that represents that object. For some reason, my data annotations aren't being rendered in the html coming down from code.

Is there any fix available to this, or is there anything different, sans having to write the validation by-hand, that I can do to solve this issue?

Thanks.

Descendent answered 20/10, 2011 at 16:58 Comment(3)
If you post some code we can have a better understanding of what are you doingBloodletting
Did you bother to read the second post - blog.stevensanderson.com/2010/01/28/…??Ortrud
Ahmed... Could use the same tone with you... I'm using mvc3, and jquery unobtrusive validation. His second post held no value to my case.Descendent
S
21

Things to consider:

  1. Data annotations will not be rendered unless a FormContext exists in whatever method you are using to create this additional object. If you are using a partial view, add the following to it at the top:

-

   if (this.ViewContext.FormContext == null) 
   {
       this.ViewContext.FormContext = new FormContext(); 
   } 
  1. If you are dynamically adding an item to the page via AJAX, then after you add your new item, you must clear the validation data in the DOM, and re-parse all of your validation elements, like so:

-

   $("form").removeData("validator");
   $("form").removeData("unobtrusiveValidation");
   $.validator.unobtrusive.parse("form");
Swap answered 20/10, 2011 at 20:18 Comment(7)
@counsellorben... Thanks for the response... I hadn't known that it was looking for the formcontext. I'll try it on Monday and see how it works. Thx!Descendent
Is there a reason for the two lines above the "parse" line??Descendent
When the unobtrusive validation attributes are parsed, data regarding the attributes are placed into the DOM. In order to re-parse correctly, the easiest solution is to clear the data from the DOM.Swap
ah... got it. didn't realize that it would clog things up in the DOM. Will have to adjust code to compensate for that. I'll see if I can setup a test project today and see it in action.Descendent
@counsellorben... Dude! you're amazing with this... well done. I was a bit shocked to see I had to implement the FormContext piece on the *.cshtml file, I would have expected to add that to the controller, but as I sit here thinking about it... that logic is specific to the View, so I guess I'm happy with that. Thanks for the help... now @ some point, I'll go back and correct the rest of the system.Descendent
@Swap But this method clear my original validation messages. Any idea how to keep that?Grimbald
Thanks for the solution.Retentive

© 2022 - 2024 — McMap. All rights reserved.