Nagaraja S
Nagaraja S

Reputation: 66

Asp.Net MVC: Hide multiple "Required" messages instead display one single error message

In one of my page, When user click submit button all of my controls "Required" message will appear in "Validation summary". Instead of showing all of these messages in the validation summary, I just want to display a single error message, which says "please fill all of these fields". example:

Instead of

<pre>
    <ul>
      <li>First Name required</li>
      <li>Last Name required</li>
      <li>Middle Name required</li>
    </ul>
</pre>

I want something like this:

<pre>
    <ul>
        <li>All fields are required</li>
    </ul>
</pre>

How can we display such message in client side?

Upvotes: 0

Views: 1452

Answers (3)

Phil Hale
Phil Hale

Reputation: 3491

Off the top of my head I can think of two approaches.

One, try using jQuery validator groups. It allows you to create a group of fields for which one error message will appear.

Two, write your own custom attribute to handle both server and client side validation. This answer provides a complete example of how to do this.

Upvotes: 0

MVCKarl
MVCKarl

Reputation: 1295

The following page will give you the answer you require. Either create a Html Helper or a partial page

Custom Validation Summary

Upvotes: 1

Shivkumar
Shivkumar

Reputation: 1903

Try This

[HttpPost]
        public ActionResult SomeAction(SomeModel model)
        {
            if (ModelState.IsValid)
            {
                return View(model);
            }
            ModelState.Clear();
            ModelState.AddModelError("", "All fields are required");
            return View(model);
        }

if your are doing validation at server side.

Upvotes: 0

Related Questions