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.