2

In a cab reservation system using MVC 4, I am using a partial view in Home->Index to display a Quick Book section. In the layout, I am using the following code to render the partial view:

    @{Html.RenderAction("CategoryMenu", "Search");} 

The CategoryMenu action of the SearchController is:

   [ChildActionOnly]
    public ActionResult CategoryMenu()
    {
        var searches = new QuickSearch();
        return PartialView(searches);
    }

The QuickSearch model is:

public class QuickSearch
{
    public int CatId { get; set; }

    [DisplayName("Pickup date")]
    [Required(ErrorMessage = "Pickup Date is  required.")]       
    public string PickupDate { get; set; }

    [DisplayName("Cab Type")]
    [Required(ErrorMessage = "Category  is  required.")]
    public int CabCategory{ get; set; }


    public static IEnumerable<Category> Categories = new List<Category> { 
new Category {
    CategoryId = 1,
    Name = "Economy"
},
new Category {
    CategoryId = 2,
    Name = "Midsize"
},
/*Other categories*/
};
}

And finally in the partial view of the CategoryMenu.cshtml file I am sending the QuickSearch model to a SearchByDate action of SearchController. In the SearchByDate action, I want to ensure that the Pickup date is not earlier than the current date. I have created a AppHelper.CheckDate() method to validate the requirement. However, I am not being able to display the error message for an earlier Pickup date in the partial view present in the Home Index. In the SearchByDate action, I tried the following:

if (!AppHelper.CheckDate(model.PickupDate))
       {

           ModelState.AddModelError("", "Date cannot be before the current date.");
           return PartialView("CategoryMenu");

       }

However, the entire CategoryMenu view is getting displayed with the error message instead of the error message that should get displayed in the partial view of the Home Index. Any help will be appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
user2693135
  • 1,206
  • 5
  • 21
  • 44

1 Answers1

0

This is because you are only showing the view after the error. Your code is this -

if (!AppHelper.CheckDate(model.PickupDate))
{
     ModelState.AddModelError("", "Date cannot be before the current date.");
     return PartialView("CategoryMenu");
}

Adding the error to the ModelState is ok. But the return part is not. You are only returning the view, this is why it is showing the view.

If (a) you only want to show the error not the view, then return only the error -

return Content("Date cannot be before the current date.")

(b) you want to show the view, with the errors, try passing the ModelsState to render the errors properly alongside with the view -

return PartialView("CategoryMenu", model);

(c) If you are showing the error in the parent view and does not want to render error or anything from this partial view then try return an empty result -

return new EmptyResult();
brainless coder
  • 6,310
  • 1
  • 20
  • 36
  • 1
    I am able to display the home page with the partial view whenever a user selects an earlier date by using 'return RedirectToAction("Index", "Home", model);' . This means user can't reserve a cab on an earlier date. However, I am not being able to display the error message. I tried 'ModelState.AddModelError' and also ViewBag.ErrorMessage in the SearchByDate action. In both the main Index and partial view, I tried displaying the error message as ' @Html.ValidationSummary(true)' and '@ViewBag.ErrorMessage;'. – user2693135 Jul 13 '14 at 05:43
  • you might wanna try passing viewdata like this to show errors in partial view - http://stackoverflow.com/questions/1169170/pass-additional-viewdata-to-a-strongly-typed-partial-view – brainless coder Jul 15 '14 at 12:00