0

I use ASP.NET MVC 2, with Visual Studio 2008 and my view is strongly-typed. The validation using ValidationAnnotation works.
What I'm trying to find is how to launch the validation when opening the form. When it opens the model has error, but error doesn't show up. When I press the submit button, the controller validates the model and return to the form.

Public Function EditVente(ByVal pNoEnreg As Integer) As ActionResult
            Dim dossierVente As VenteDansMedianePlus = model.Helper.selectDossierVente(pNoEnreg)

        Return View(dossierVente)
    End Function

    Public Function enregistrerVente(ByVal pVente As VenteDansMedianePlus) As ActionResult
        If ModelState.IsValid Then
            model.Helper.updateDossierVente(pVente)
            Return RedirectToAction("EditVente", "A009P003", New With {Key .pNoEnreg = pVente.noEnreg})
        Else
            Return View("EditVente", pVente)
        End If

    End Function

I try to put ModelState.IsValid in the editVente function, but it doesn't work.

My question is how to launch the model validation before returning the view, so the view has the error message.

tereško
  • 58,060
  • 25
  • 98
  • 150
fanfaron
  • 31
  • 8
  • I just found this http://stackoverflow.com/questions/5347827/validation-messages-are-displayed-when-page-load. That person having my solution. – fanfaron May 30 '12 at 03:23

1 Answers1

0

I think you're looking for the TryUpdateModel-method. You can call this method on your controller and pass in your model: (Sorry, I'm a C# developer, hope this is correct)

Public Function EditVente(ByVal pNoEnreg As Integer) As ActionResult
     Dim dossierVente As VenteDansMedianePlus = model.Helper.selectDossierVente(pNoEnreg)

     'Update the validation
     TryUpdateModel(dossierVente)

     Return View(dossierVente)
End Function

For more information, check this SO post: When and why do you use TryUpdateModel in asp.net mvc 2?

Edit

The TryUpdateModel-method works about the same as the UpdateModel-method, except that UpdateModel throws errors when the validation fails, where TryUpdateModel doesn't.

Community
  • 1
  • 1
Pbirkoff
  • 4,642
  • 2
  • 20
  • 18
  • Hi, I tried but it give me a error that my object can't be used in tryUpdateModel... But I have to admit that I use updateModel and not tryUpdateModel. I will try it again. If the framework did it easily it should be simple, I'm thinking. – fanfaron May 30 '12 at 02:52
  • I will try to call updateModel, but it seems to me that I tried it and it give me a error, I post it tomorrow. – fanfaron May 30 '12 at 03:26
  • found this http://stackoverflow.com/questions/9832742/validation-firing-on-page-load. It seems if I can pass a fake parameter it will trigger the model binding mechanism. – fanfaron May 30 '12 at 03:53
  • Thanks, tryUpdateModel did work like advertised :). When using updateModel it give this error: "The model of type 'ProportionMediane.VenteDansMedianePlus' could not be updated" I thought updateModel and tryUpdateModel is the same except that tryUpdateModel throws a exception when it fails. Many thanks again first thing I tried this morning and voila it works. – fanfaron May 30 '12 at 11:33
  • Good to hear. UpdateModel indeed throws exceptions. I'll add this to my answer. – Pbirkoff May 30 '12 at 12:55
  • Thanks again, now I understand, If I wanted to work with updateModel, I should have handle the exception. Thanks again for clearing it up. – fanfaron May 30 '12 at 17:11