I use an O/R mapper, that can reload objects from the DB in a generic manner. I would like to be able to intercept the request right after the creation of the mapped objects, in order to reload them.
我使用O / R映射器,可以通用方式从DB重新加载对象。我希望能够在创建映射对象后立即拦截请求,以便重新加载它们。
ActionFilters are of course there, yet the problem is that ActionFilters (or the examples I have seen) can handle the data as provided by the form and not after an object had been created.
ActionFilters当然在那里,但问题是ActionFilters(或我见过的例子)可以处理表单提供的数据,而不是在创建对象之后。
I looked at the overridable methods of the Controller, but found nothing obvious that caught my eyes. Does any one know of a way to do this?
我看了控制器的可覆盖的方法,但没有发现任何明显的东西引起了我的注意。有没有人知道这样做的方法?
Thank you for your suggestions!
谢谢你的建议!
Nasser
2 个解决方案
#1
What you need is a custom model binder. You can inherit from the default ModelBinder class and provide the logic you want.
您需要的是自定义模型绑定器。您可以从默认的ModelBinder类继承并提供所需的逻辑。
For example, if you have this:
例如,如果你有这个:
public ActionResult Save([Bind(typeof(CustomModelBinder))] Customer customer)
{
/* ... */
}
The model binder will create the object for you, but you can choose to fetch it from the database first (for existing records).
模型绑定器将为您创建对象,但您可以选择首先从数据库中获取它(对于现有记录)。
#2
If I right understand you need something like this.
如果我理解你需要这样的东西。
public class Navigate : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
ViewResult view = filterContext.Result as ViewResult;
}
}
In view you will find view.ViewData.Model that belongs to current Controller and you can do all that you want with this mapped data. Let me know if I was right :)
在视图中,您将找到属于当前Controller的view.ViewData.Model,您可以使用此映射数据执行所需的所有操作。如果我是对的,请告诉我:)
#1
What you need is a custom model binder. You can inherit from the default ModelBinder class and provide the logic you want.
您需要的是自定义模型绑定器。您可以从默认的ModelBinder类继承并提供所需的逻辑。
For example, if you have this:
例如,如果你有这个:
public ActionResult Save([Bind(typeof(CustomModelBinder))] Customer customer)
{
/* ... */
}
The model binder will create the object for you, but you can choose to fetch it from the database first (for existing records).
模型绑定器将为您创建对象,但您可以选择首先从数据库中获取它(对于现有记录)。
#2
If I right understand you need something like this.
如果我理解你需要这样的东西。
public class Navigate : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
ViewResult view = filterContext.Result as ViewResult;
}
}
In view you will find view.ViewData.Model that belongs to current Controller and you can do all that you want with this mapped data. Let me know if I was right :)
在视图中,您将找到属于当前Controller的view.ViewData.Model,您可以使用此映射数据执行所需的所有操作。如果我是对的,请告诉我:)