Thursday, April 21, 2011

Check for ModelState errors in posted FormCollection on different Controller

I have a WebPage Controller that handles all my public side of things on the website.

I have an Enquiry table to insert details of information typed in a contact form.

On the contact page it posts to the WebPage Controller with the contact form information.

How do I check that the form collection validates against the expected Enquiry table? My model has DataAnnotations if that helps.

Thanks

UPDATE: I realised I can do a TryUpdateModel but to put the information into my object but how do I passback the validation information eg/they didn't type in their name

From stackoverflow
  • Have you checked out how Nerd Dinner does it? They extend the model with a validate method which checks values and build a validation errors collection that can then be passed back to the view to render.

    It's quite a neat solution.

    Check this out

    And this

    And this might help

    Jon : Problem is I dont want to manually check every field and then add to modelstate. After I do TryUpdateModel is there not something I can do which checks the DataAnnotations requirements and then it determines that ColumnA is missing then I can add to ModelState
  • I discovered that even though I do a TryUpdateModel on a different model it would get returned in the modelstate so I used a prefix and so validationsummary would pick it up

0 comments:

Post a Comment