Faust
Faust

Reputation: 15404

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?

Upvotes: 16

Views: 3828

Answers (1)

ek_ny
ek_ny

Reputation: 10243

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

ModelState.AddModelError(String.Empty, "here is the error");

Upvotes: 23

Related Questions