Ok. I found a very convincing answer as follow:
I started to read about the MVC5 life-cycle and found many articles on the net. one of them is the following link: http://www.dotnet-tricks.com/Tutorial/mvc/TbR0041112-Asp.net-MVC-Request-Life-Cycle.html so I copied the picture and added my comment on it as the following [courtesy: www.dotnet-tricks.com]
then I read in another article [ here: http://www.codemag.com/Article/1312081] , how to render the view to string and returned that as the return type of the action method. so that I may be able to use the lazy loading and render the view while still inside the using statement.
so all I did was the following change to my action method [ explanation is included as comments]
// GET: /dept/
public string Index()
{
IView myView;
string result;
using (var ctx = new ApplicationDbContext())
{
//my model brought using the dbContext
IEnumerable<department> d = ctx.departments;
// now because I want to render the View here [forcibly] and not waiting
//for the normal MVC pipeline to render my View I had to jump to the ViewEngine
//and ask it to render my View [while i am still inside this using statement]
// so referring to the excellent article on :http://www.codemag.com/Article/1312081
//I did the following:
ControllerContext.Controller.ViewData.Model = d;
ViewEngineResult viewEngResult = ViewEngines.Engines.FindView(ControllerContext, "~/Views/dept/Index.cshtml", null);
myView = viewEngResult.View;
//till this point the View is not rendered yet
StringWriter tw = new StringWriter();// used to render the View into string
ViewContext vc = new ViewContext(ControllerContext, myView, ControllerContext.Controller.ViewData, ControllerContext.Controller.TempData, tw);
//it is the following method .Render(viewContext, textWriter) that will start iterating on the IEnumerable<department> object
myView.Render(vc, tw);
result = tw.ToString();// the rendered View is now written as string to the result
tw.Dispose();
}
return result;
}
}
and I was happy to see that my page rendered successfully without that famous disposing error; see the result:
So to sum it up:
the answer to my question is :
when you return a ViewResult or ActionResult from your action method; the view is still not rendered. and once it reach in the pipeline to the ViewEngine and the ViewEngine trigger the method .Render(), it is at that time the lazy loading object will need the dbContext and will result in the famous Disposing error of the dbContext. I also showed how can you render the View inside the action method itself. and even inside the using statement of the dbContext; and I could escape that disposing error.
thank you for everyone :)