This is my class which is basically used for UnitForWork pattern i.e save everything in a transaction:
public class TestFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
if (filterContext.HttpContext == null)
throw new NullReferenceException("null");
else
{
ObjectContext objectContext = (ObjectContext)filterContext.HttpContext.Items
[ObjectContextManager.TestContext];
if (objectContext != null)
{
objectContext.SaveChanges();
}
}
}
}
This works fine. However, I also want to make sure that it is only saved if the ModelState.IsValid property is true in my action method. How can I do so?
filterContext.Controllergives you reference ofControllerBaserather thanController. if you cast it toControllerit will give you access toModelStatewhich is a public property of controller class likeCurrently, i have no idea what are the implications of this casting. Please inquire a bit about consequences before using.
UPDATE:
you can also access Modelstate property like
and it involves no casting