This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, I apologise ( for all the questions I keep asking) and if this is a naff question. My excuse: I'm a database person and learning .net, MVC, Lightspeed and LINQ Controller: I have a parent method that calls a child method. The child method uses a Unit of Work to create a SelectList. This is then passed back to the parent method. From the parent method, the SelectList is passed to the View (via ViewData). I get an error of: "Cannot access a disposed object" on generation of the view. I realise that this is because the Unit of Work has been disposed of and so the LINQ query is also disposed of, however, what techniques would you use to pass the SelectList back to the parent? I've seen threads on using the Unit of Work "Attach" method but can't get it to work in my parent method. Here is my child method: public SelectList getTitlesSelectList() { SelectList titlesSelectList; using (var uow = LightSpeedContext.CreateUnitOfWork()) { var data = from t in uow.Titles orderby t.ListSeq select new { code = t.Code description = t.Description }; titlesSelectList = new SelectList(data,"code","description") } return titleSelectList; }
Many thanks
|
|
|
Call ToList() or another method that forces execution on your on your linq IQueryable before passing it into the SelectList. LINQ queries don't execute until enumerated, which is happening later in the request lifecycle, after the UOW has been disposed. |
|
|
Ah! I see. That works a treat! Thanks kdages, much appreciated. |
|