2

I have a link in page, that when clicked by a user sends a post to the Controller. The controller accepts a single int value. This is the id of the item in the View.

The Model in the View is an IEnumerable Type.

I need my controller to do a check on the item and validate it against another model. This is a custom model and has no Views. I have tried:

@Html.ValidationSummary(true, "Validation Summary Error!")

It does not work because I am using an IEnumerable Type.

My Question is, how do I send a message back to the View from the Controller if a situation is met in the Controller?

[EDIT] I am using a WebGrid to display the items. This is why I am using an IEnumerable Type Model in my View.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55

1 Answers1

4

The ValidationSummary will display all ModelState errors, so you can add random ones with an empty key for situations like this:

if (!condition) {
    ModelState.AddModelError("", "Validation error!");
}
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • How does this return the message to the page? – Rusty Nail Sep 17 '13 at 02:10
  • 1
    The `ValidationSummary` will display it. – Simon Whitehead Sep 17 '13 at 02:11
  • Sorry Simon, I said I cant use the ValidationSummary because I am using IEnumerable Type and I cant cast it. – Rusty Nail Sep 17 '13 at 02:17
  • I see your post here: http://stackoverflow.com/questions/11775602/when-use-ienumerable-in-view-client-side-validation-is-not-working but because I am using a webgrid I cant do it that way, at least without a total re-write. – Rusty Nail Sep 17 '13 at 02:34
  • I don't understand why you can't use ValidationSummary.. is your question not just "how do I display xxx random error message in view?" – Simon Whitehead Sep 17 '13 at 02:50
  • In normal situation Yes, you're exactly right. In your answer I linked to above, if its Strongly Typed all will work. As soon as the Model is List or IEnumerable Types the Validation cant cast to the item or object. As yet I still don't understand why. Because I am using a WebGrid, the Model needs to be enumerable Type, either List or IEnumerable Type... But Validation cant cast to that. – Rusty Nail Sep 17 '13 at 03:00
  • Why are you casting validation? I don't understand what you mean by "validation can't cast to that". Can you give us an example of "casting validation" in your question? – Simon Whitehead Sep 17 '13 at 03:14
  • It does it itself. I am not doing it. – Rusty Nail Sep 17 '13 at 03:30
  • Or at least that's what its asking for, the error is a casting issue. – Rusty Nail Sep 17 '13 at 03:39
  • Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. CS1973: 'System.Web.Mvc.HtmlHelper>' has no applicable method named 'ValidationMessage' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. – Rusty Nail Sep 17 '13 at 05:42
  • I know, ValidationSummary is not ValidationMessage or ValidationMessageFor<> but I would like to define the error better. – Rusty Nail Sep 17 '13 at 05:43
  • I do apologise, I am understanding this issue a little better now and see I have not been clear. – Rusty Nail Sep 17 '13 at 05:48
  • This also explains my issue but I don't see how to solve it in my case: http://stackoverflow.com/questions/5832692/html-actionlink-cannot-be-dynamically-dispatched – Rusty Nail Sep 17 '13 at 06:03