1

Am having issues loading navigation(child ) element from a controller to a view

I created this model

public class MultipleItems
{
    public IEnumerable<Order> Orders { get; set; }
    public IEnumerable<support> Supports { get; set; }
    public IEnumerable<DbModel.Track> Tracks { get; set; }
    public IEnumerable<Receipt> Receipts { get; set; }
    public IEnumerable<Quota> Quotas { get; set; }
}

And the controller

public ActionResult Client()
{
    string useremail  = User.Identity.GetUserName();
    var  myModel = new MultipleItems();
    myModel.Supports = new SupportW().GetSupports(useremail);
    myModel.Orders = new Orders().GetOrders(useremail);
    myModel.Receipts = new Receipts().GetReceipts(useremail);
    myModel.Tracks = new Track().GetTracks(useremail);
    myModel.Quotas = new Quotas().GetQuota(useremail);
    return View(myModel);
}

Am interested in Tracks() and this is the method

public IEnumerable<DbModel.Track> GetTracks(string email)
{
    try
    {
        using (var da = new CourierEntities())
        {
            da.Tracks.Include(a => a.Order);
            da.Tracks.Include(a => a.Order.User);
            da.Tracks.Include(a => a.destination);
            da.Tracks.Include(a => a.Source);
            da.Tracks.Include(a => a.CurrentLocation);
            var q = (from p in da.Tracks where p.Order.User.Email == email select  p).ToList();
            if (q != null)
            {
                return q;
            }
        }
        return null;
    }
    catch (Exception ex)
    {
        returnMsg = ex.Message;
        return null;
    }
}

While the view is

<td>
    @if (Model.Orders.Count() != 0)
    {
        foreach (var i in Model.Tracks)
        {
            <tr>
            Order id      @i.OrderID
            <br />    
            Trackid    @i.TrackID
            <br />  
            @i.Order.Packagename 
            </tr>
        }  
    }
</td>

am having issues with @i.Order.Packagename and i recieve this error

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Source Error:  
Line 120:
Line 121:                                          <br />  
Line 122:                                          @i.Order.Packagename 
Line 123:                                      </tr>
  • Read [link] (https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) and [link] (https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-3) – MCoder May 08 '18 at 12:50
  • It seems that you need to use view model concept. Search google for mvc use view model – MCoder May 08 '18 at 12:51

1 Answers1

1

Although the comments suggest the use of a View Model, which would be a good idea, the fundamental issue with your code is the lifetime of the database context(s).

When you execute code such as:

myModel.Supports = new SupportW().GetSupports(useremail);

The object SupportW has a lifetime of that statement. It will likely be disposed before your view executes. If the method GetSupports(useremail) returns IQueryable then when that is resolved in the view, the source is no longer there.

To resolve this you should change the lines to:

myModel.Supports = new SupportW().GetSupports(useremail).ToList();

The addition of the ToList() resolves the `IQueryable' immediately and the data will be available in your view.

Philip Smith
  • 2,741
  • 25
  • 32