I am using ASP.NET MVC for developing a web site. I am using jquery for AJAX functionality. In the action methods, I want to return some error to signal that the input is not correct or that the action could not be performed. In such error cases, I expect the jquery ajax error handler to be called and I can take appropriate action in there. I have not found a way how to do this. Following is my action method.
In error cases, what should I be sending from an Action in order to get the jquery error handler triggered?
public ActionResult AddToFavourites(int entityId, string entityType)
{
if (!Request.IsAjaxRequest())
throw new InvalidOperationException("This action can be called only in async style.");
try
{
RBParams.EntityType typeOfFavourite = (RBParams.EntityType)Enum.Parse(typeof(RBParams.EntityType), entityType);
string status = "";
if (typeOfFavourite == RBParams.EntityType.BusinessEntity)
{
status = MarkFavouriteEntity(entityId);
}
else if (typeOfFavourite == RBParams.EntityType.Review)
{
status = MarkFavouriteReview(entityId);
}
else
{
throw new InvalidOperationException("The type of the entity is not proper");
}
return Content(status);
}
catch (Exception ex)
{
return Content("Error");
}
}