Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top