Link to home
Start Free TrialLog in
Avatar of Niall Gallagher
Niall GallagherFlag for Ireland

asked on

ValidationSummary is not displaying

Hi Experts,
I have done this multiple times and it usually works with no problems but for the life of me I can figure out what I'm doing wrong this time.
I'm calling my action to enter data into the DB but first I run validation on it and I know that it is failing validation as I have first name and last name as required.
 public ActionResult Create([Bind(Include ="F_Name,L_Name,HireDate,LAN_ID,Office_ID")] Employee e)
        {
            try
                {
                    HRRepository RR = new HRRepository();
                 if(!ModelState.IsValid)                 
                    {
                        var allerrors = ModelState.Values.SelectMany(v => v.Errors).ToArray();
                        foreach (ModelError error in allerrors)
                        {
                            ModelState.AddModelError("", error.ErrorMessage.ToString());
                        }
                        return View("Index");      
                    }
                 else
                   {
                       RR.Enter_NewHire(e);
                       ViewBag.ShowPartial = true;
                       return View("Index");  
                  }
                }
                catch(Exception ex)
                {
                    return View("Error", new HandleErrorInfo(ex, "App", "Create"));
                }

Open in new window


I can watch the 2 messages being attached as it runs in debug mode but when the page is displayed the is no validation summary

Here is the top of the view
@using (Html.BeginForm("Create", "NewHires", FormMethod.Post))
{
    @Html.AntiForgeryToken()   
    @Html.ValidationSummary(false, "Please Correct Errors")
     <div id="divmain" style="margin-left:10em;">
    <fieldset>

Open in new window


I use jquery while passing the data to the action but I have done this multiple times before. The problem is probably staring me in the face but I just can't see it.
Any help would be greatly appreciated.
Thanks in advance
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

You are not specifying the control for which you are putting error message

ModelState.AddModelError("controlName", Message);

Open in new window

Avatar of Niall Gallagher

ASKER

When putting the error message in the validation-summary you do not put in a name.
You used to have to put in string.Empty() but in MVC 4 (or possibly 3) they changed it so you just had to put in ""
ASKER CERTIFIED SOLUTION
Avatar of Niall Gallagher
Niall Gallagher
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This solution works good to show the user that the data did not validate on client side which would do my job because it is an intranet  site.