我在几个地方编写这段代码,并且总是重复这个逻辑:
public ActionResult MyMethod(MyModel collection)
{
if (!ModelState.IsValid)
{
return Json(false);//to read it from javascript, it's always equal
}
else
{
try
{
//logic here
return Json(true);//or Json(false);
}
catch
{
return Json(false);//to read it from javascript, it's always equal
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用动作过滤器,不被重复try-catch,请问如果模型是有效的,并return Json(false)作为ActionResult?