【发布时间】:2011-01-23 14:18:45
【问题描述】:
我正在使用 ASP.NET MVC 开发网站。我正在使用 jquery 来实现 AJAX 功能。在操作方法中,我想返回一些错误以表明输入不正确或无法执行操作。在这种错误情况下,我希望调用 jquery ajax 错误处理程序,并且我可以在那里采取适当的措施。我还没有找到如何做到这一点的方法。以下是我的操作方法。
在错误情况下,我应该从 Action 发送什么来触发 jquery 错误处理程序?
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");
}
}
【问题讨论】:
标签: jquery asp.net ajax asp.net-mvc