简体   繁体   中英

Custom Validation Message from Controller to View

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.

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!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM