Sunday, 15 July 2012

c# - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection in mvc5 view -



c# - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection in mvc5 view -

i'm trying loop in mvc5 view particular model properties i'm getting error:

additional information: objectcontext instance has been disposed , can no longer used operations require connection.

this view have:

@using app.models @model app.portal.webui.models.manageviewmodel @{ viewbag.title = "edit"; } <h2>edit</h2> <div class="container"> <div class="col-md-9"> <div class="col-md-12"> <div class="form-group"> @html.labelfor(m => m.auser.id) <label>@html.displayfor(m => m.auser.id)</label> </div> <div class="form-group"> @html.labelfor(m => m.auser.firstname) @html.textboxfor(m => m.auser.firstname) </div> <div class="form-group"> @html.labelfor(m => m.auser.phonenumber) @html.textboxfor(m => m.auser.phonenumber) </div> <div class="form-group"> </div> </div> <div class="col-md-12"> <div class="form-group">&nbsp;</div> <div class="form-group"> @html.labelfor(m => m.auser.lastname) @html.textboxfor(m => m.auser.lastname) </div> <div class="form-group"> @html.labelfor(m => m.auser.email) @html.textboxfor(m => m.auser.email) </div> </div> </div> <div class="col-md-3"> @foreach (aspnetrole userrole in model.auser.aspnetroles) { @html.checkbox(userrole.name, true) @html.label(userrole.name)<br /> } </div> </div>

the error happens in foreach loop.

any thought doing wrong?

thanks lot, laziale

update (code added):

manageviewmodel model = new manageviewmodel(); if (id.hasvalue) { using (var db = new dbcontext()) { aspnetuser user = (from p in db.aspnetusers p.id == id select p).first(); model.auser = user; list<aspnetrole> roles = (from r in db.aspnetroles select r).tolist(); model.rolelist = roles; } }

@foreach (aspnetrole userrole in model.rolelist) { @html.checkbox(userrole.name, true) @html.label(userrole.name)<br /> }

don't utilize entities connected db in viewmodel! never!! causes issues have encountered. separate layers. read separation of concerns separation of concerns

c# asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5

No comments:

Post a Comment