views:

116

answers:

2

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.

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.

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

A: 

Hi. 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 :)

omoto
Dear Omoto!thank you for your reply.You answer deals with the result AFTER the action. I am actually interested in getting at the mapped objects prior to the action being hit. GreetingsNasser
Nasser
A: 

What you need is a custom model binder. You can inherit from the default ModelBinder class and provide the logic you want.

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).

Ben Scheirman
Dear Ben!Thank you for your reply! I found the code to the modelbinder that comes with the O/R Mapper I use, so I now must try and work out the reload within the modelbinder!Thank you for your reply!Nasser
Nasser