0

Im working in a project where the validations messages with unobtrusive system works fine.

When any attribute is uncompleted or wrong the proper error messages appear, at client side ofc.

The problem is the following. I need to set an general error message at the top of the site (besides to each field validation message error). I know i can use @Html.ValidationSummary but i dont want the list of fields, what i need is one general message telling the user that there is something wrong with the form.

Regards.

Edit: On jquery before submit i'm doing $("#frmSubmit").valid();

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
pata
  • 959
  • 2
  • 18
  • 35
  • @Html.ValidationSummary(true, "Error") and i cant get the "error" message when there is an input error. – pata Jul 25 '14 at 14:22
  • What i did, if anyone want to know : [link][1] thx @Marko. – [1]: http://stackoverflow.com/questions/5052315/how-to-fire-jquery-function-only-if-form-is-valid – pata Jul 25 '14 at 19:36

2 Answers2

0

Did you try using the ValidationSummary(bool) overload? Supplying true excludes property errors.

Dean Ward
  • 4,793
  • 1
  • 29
  • 36
  • i did, and i get no message at all. Even if i set the message @Html.ValidationSummary(true, "Error") – pata Jul 25 '14 at 14:21
  • 1
    So your general message needs to be added to your `ModelState` with `ModelState.AddModelError(string.Empty, "My general error");`. That'll then render in the validation summary... – Dean Ward Jul 25 '14 at 14:24
  • See http://stackoverflow.com/questions/2818219/asp-net-mvc-html-validationsummarytrue-does-not-display-model-errors for an example. – Dean Ward Jul 25 '14 at 14:25
  • Where do i set this model error? I get every @Html.ValidationMessageFor() activated before reaching the controller. – pata Jul 25 '14 at 14:40
  • In your controller: if (!ModelState.IsValid) { ModelState.AddModelError(string.Empty, "My general error"); } or a filter if you want to re-use it. – Dean Ward Jul 25 '14 at 14:44
  • Well, i never get to the controller as i'm having client side validation with the unobtrusive. – pata Jul 25 '14 at 14:51
  • Gotcha, what @Marko suggests is probably the best bet for dealing with this client-side. – Dean Ward Jul 25 '14 at 16:17
0

Why not just have check ModelState on your view like this:

@if (!ViewContext.ViewData.ModelState.IsValid)
{
    <div>Your error message here...</div>
}

Then style your div to be errorish...

Marko
  • 12,543
  • 10
  • 48
  • 58
  • 1
    Ahhh never mind you want it to work with unobtrusive validation. Did you try this: http://stackoverflow.com/questions/5052315/how-to-fire-jquery-function-only-if-form-is-valid. Just instead of the alert display some div.... – Marko Jul 25 '14 at 15:10