ASP.NET MVC: run code after view has rendered (close db transaction)

Posted by thermal7 on Stack Overflow See other posts from Stack Overflow or by thermal7
Published on 2010-03-31T00:17:17Z Indexed on 2010/03/31 0:23 UTC
Read the original article Hit count: 255

Filed under:
|

Hi,

I am using ASP.NET MVC2 with NHibernate, but am facing an issue. All calls to the database via NHibernate should be inside a transaction, however code inside the view kicks off database calls in some instances. Thus there is a need to be able to commit the transaction after the view has rendered.

For example displaying a list of users and their user roles you might show the user role using this code: <%: Model.UserRole.Name %>

This will cause a hit on the database as the UserRole is loaded using a NHibernate proxy.

You can fetch the UserRole eagerly which circumvents the issue in this case, but there are cases where it is much faster to use lazy loading.

Anyway, is there a way to run code after a view has rendered?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about nhibernate