Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model-level errors in MVC3?

I appear to have missed something very simple, but can't find the answer anywhere.

I want to generate a generic "You've got errors -- see below" type of statement atop a form when any errors exist using @Html.ValidationSummary(true), and list all the specific errors only inline next to the relevant fields. I want the logic and the error text contained in an Action Filter.

The problem is, all the documentation I've seen on "model-level" errors explains how to display them in views, but not how to add them to the model state.

Errors added via ModelState.AddModelError are not model-level - so how can I do I add a model-level error?

like image 336
Faust Avatar asked Dec 02 '11 16:12

Faust


1 Answers

ModelState.AddModelError will work. Just set the key with an empty string.

ModelState.AddModelError(String.Empty, "here is the error");
like image 148
ek_ny Avatar answered Sep 21 '22 15:09

ek_ny