Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 377213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:40:28+00:00 2026-05-12T14:40:28+00:00

I use custom model binder to bind Order/OrderItem in my actions. This model binder

  • 0

I use custom model binder to bind Order/OrderItem in my actions. This model binder uses ServiceLocator.Current.GetInstance(); where ICart in turn depends on IOrderRepository (not sure if this matters).

Now, everything works fine when I create first order item. And when I create the second one. Then I try to display the order which now contains two order items. This is done using

  public ActionResult Show(Order order) {}

where order is bound by my custom binder. I trace its BindModel and see, that after the call to

  order = cart.GetOrder(id);

order items are OK – i.e. I add the to Watch window, view properties, Products, and they’re all OK.

However, when the control flow comes to the Show(Order order) action method, the order’s first item has invalid Products – accessing them results in following error (the famous one):

NHibernate.LazyInitializationException: Initializing [Orders.Core.OrderItem#5440c233-fb7e-4dc9-8aec-9c8c0115808b]-failed to lazily initialize a collection of role: Orders.Core.OrderItem.Products, no session or session was closed

I can now see this in the Watch window when viewing order.Items[0].Products.

The weird thing, is that the second order item is still OK! So if it goes like

  • HTTP request … My Model Binder – get order using orderRepository.Get(id) – session is OK for both order items …
  • mvc does its magic
  • action method Show(Order order) <- here session for order.Items [0].Products is lost, while for Items[1] is not

If I add another item to the order, then in the Show(), only Item[2].Products are correct, both Item[0] and Item[1] Products are bad (no session).

What’s going on here?

I use Sharp Architecture and Session per request. I actually verified that EndRequest is not called between model binder getting its order and Show() receiving incorrect one.

UPDATE: some code (important lines)

public class Cart : ICart
{
      public Cart(IOrderRepository orderRepository, ICurrentUser currentUser, IUserSessionStorage storage) {}
      public Order GetOrder(Guid id)
      {
         return orderRepository.Get(id);
      }
}

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
  var cart = ServiceLocator.Current.GetInstance<ICart>();
  //Guid guidId = new Guid(id_value_from_context);
  var order = cart.GetOrder(guidId); // here order is OK completely
  return order;
}

OrderRepository is S#arp Architecture repository and uses WebSessionStorage class, which only closes Session at the EndRequest handler. As I said I’ve verified it is NOT get called between order is OK and order is not.

Update: I wonder if this can be because of additional manual transaction around orderRepository.Save(order). Will investigate tomorrow, but found something similar here on SO.

UPDATE: Moreover, this happens only after this:

cart.Save(item);
return RedirectToAction<OrdersController>(c => c.Show(item.Order));

When I then go to the address bar and press “Enter” to reload the page, it works correctly. So it’s one-time issue.

What’s funny, there’s no such bug if I do:

return RedirectToAction("Show", "Orders", new { order = item.Order });

So it’s something with MvcContrib trying to deal with order in TempData, I’d say…

I found that I have this on controllers:

   [PassParametersDuringRedirect]
   public class OrdersController

and thought that my order parameter comes from TempData instead of ModelBinder… though model binder did fire, too. But when I removed the attribute, the problems did not disappear. So it’s something with RedirectToController<> from MvcContrib but I have no idea why this happens.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:40:29+00:00Added an answer on May 12, 2026 at 2:40 pm

    Oh my god, I’ve put [PassParametersDuringRedirect] on my BaseController. I really did it, during the time when I experimented with MvcContrib, and just forgot about it. And it came back and bite me so bad, I’ve lost several hours trying to solve this.

    So, the problem is me being stupid, and the lesson learned, is that one should never do system-wide attributes/behavior, since this is no better than global variables in sense of side-effects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the MVC 2 RC. I have a custom model binder that used
Here's simple view model that I use: public class ViewModel { public Order Order
I have an app that makes use of 'django-registration' a custom built model named
I've got a JTable which model is custom that extends AbstractTableModel. In this the
I have a DateTime field in my Model. If I try to use this
Can I have a custom service providing the storage of the models I use
I'm trying to use custom container widgets in gtk.Builder definition files. As far as
I am using wordpress and use custom permalink structure: /%category%/%postname%/ My problem is that
I use a custom Matrix class in my application, and I frequently add multiple
I use a custom-built asp.net control that renders to a DIV and has height='0'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.