MVC View not displaying validation errors

10,592

The problem is that you are making a new request with RedirectToAction so the validation of your model set to valid. To solve your problem you need to do like this:

[HttpPost]
public ActionResult StopScheduled([Bind(Prefix = "item")]  BillPayModel model)
{
         try
         {
             if (ModelState.IsValid)
             {
                //save stuff into db
                 db.SaveChanges();
             }
             else
             {
                 ModelState.AddModelError("", "Could not Stop Scheduled Payment");
             }
         }
         catch (FormatException)
         {
             ModelState.AddModelError("", "Could not Stop Scheduled Payment");
         }
         return View(model);
}

And in the view you should change the Html.ValidationSummary excludePropertyErrors to false, like this:

if (Model.IsSuccess == true)
{
    <span><center><font color = "green">Successfully Completed Transaction!  </font></center></span>
}
else
{
     @Html.ValidationSummary(false, "ERROR! Please make sure you have entered correct details"); 
}

To show them oneach input you need yoo change your if statement (Model==null).

Share:
10,592

Related videos on Youtube

kayze
Author by

kayze

Updated on October 13, 2022

Comments

  • kayze
    kayze over 1 year

    i've got some validations in my mvc project. However, when i sumbit an empty form or form where some required fields have not been entered, it does not stay within the same form/view and show the errors. I cant see any modal errors (i.e amount is required field)

    for example, here are some attributes in my modal

        [Required]
        [StringLength(1, MinimumLength = 1)]
        public string Period { get; set; }
    
        [Required]
        [DataType(DataType.DateTime)]
        public System.DateTime ModifyDate { get; set; }
    

    Here is my controller

    [HttpPost]
             public ActionResult StopScheduled([Bind(Prefix = "item")]  BillPayModel model)
             {
                 //getUsers();
    
                 try
                 {
                     if (ModelState.IsValid)
                     {
    
                        //save stuff into db
                         db.SaveChanges();
    
                     }
                     else
                     {
                         ModelState.AddModelError("", "Could not Stop Scheduled Payment");
                     }
                 }
    
    
                 catch (FormatException)
                 {
                     ModelState.AddModelError("", "Could not Stop Scheduled Payment");
                 }
    
    
                 return RedirectToAction("StopScheduled");
             }
    
        }
    

    here is my view

    @if (Model !=null)
    {
        if (Model.IsSuccess == true)
        {
            <span><center><font color = "green">Successfully Completed Transaction!  </font></center></span>
        }
        else
        {
             @Html.ValidationSummary(true, "ERROR! Please make sure you have entered correct details"); 
        }
    }
    
    @if (Model ==null)
    { 
        using (Html.BeginForm("BillPay", "BillPay", FormMethod.Post, new {}))
        {
            @Html.ValidationSummary(true);
    
            <div>@Html.LabelFor(model => model.AccountNumber)</div>
             @Html.DropDownList("Accounts",  "-- Select User --")  
    
            <div>@Html.LabelFor(model => model.PayeeID)</div>
            @Html.DropDownList("PayeeID",  "-- Select User --")         
    
    
            <div>@Html.LabelFor(model => model.Amount)</div>
            <div>@Html.TextBoxFor(model => model.Amount,new {style = "width:150px"})
                @Html.ValidationMessageFor(model => model.Amount)
            </div>        
    
            <div>@Html.LabelFor(model => model.ScheduleDate) (i.e 20/10/2013 10:00)</div>
            <div>@Html.TextBoxFor(model => model.ScheduleDate,new {style = "width:250px"})
                @Html.ValidationMessageFor(model => model.ScheduleDate)
            </div>
    
            <div>@Html.LabelFor(model => model.Period)</div>
            <div>@Html.TextBoxFor(model => model.Period,new {style = "width:150px"})
                @Html.ValidationMessageFor(model => model.Period)
            </div> 
    
            <input type="submit" value ="Submit" style="width:8%;height:5%"/> 
            <input type="reset" value ="reset" style="width:8%;height:5%"/>   
        }
    }
    else
    {
    
    }
    
    • Silvermind
      Silvermind over 10 years
      Did you include jquery.validation.js in you (master)page?
  • kayze
    kayze over 10 years
    this didnt really resolve it. all i still get is "ERROR! Please make sure you have entered correct details". WHy is my from not been displayed when i submit with that error?
  • Rikard
    Rikard over 10 years
    @kayze Yes ofc it only shows that because you dont show your validation message if model is not null.