我有两个控制器:
public class AController : Controller
{
public ActionResult AControllerAction()
{
if (// BControllerAction reported an error somehow )
{
ModelState.AddModelError("error-key", "error-value");
}
...
}
}
public class BController : Controller
{
public ActionResult BControllerAction()
{
try{Something();}
catch(SomethingExceprion)
{
// here I need to add error info data,
// pass it to AController and redirect to
// AControllerAction where this error will be added
// to model state
}
}
}
我想我可以做类似的事情:
public ActionResult BControllerAction()
{
try{Something();}
catch(SomethingException)
{
var controller = new AController();
controller.ModelState.AddModelError("error-key", "error-value");
controller.AControllerAction();
}
}
但我建议这将是打破架构的方法,我不想那样做。除了传递模型对象之外,还有一些更简单、更安全的方法吗?