c# - The model item passed into the dictionary is of type 'System.Linq.GroupedEnumerable`3 but this requires 'System.Collections.Generic.IEnumerable`1 -


i following error when trying run application:

the model item passed dictionary of type 'system.linq.groupedenumerable'3[webapplication1.question,system.int32,webapplication1.question]', dictionary requires model item of type 'system.collections.generic.ienumerable`1[webapplication1.question]'.

here stack trace:

[invalidoperationexception: model item passed dictionary of type 'system.linq.groupedenumerable`3[webapplication1.question,system.int32,webapplication1.question]', dictionary requires model item of type 'system.collections.generic.ienumerable`1[webapplication1.question]'.]    system.web.mvc.viewdatadictionary`1.setmodel(object value) +175    system.web.mvc.viewdatadictionary..ctor(viewdatadictionary dictionary) +107    system.web.mvc.webviewpage`1.setviewdata(viewdatadictionary viewdata) +49    system.web.mvc.razorview.renderview(viewcontext viewcontext, textwriter writer, object instance) +99    system.web.mvc.buildmanagercompiledview.render(viewcontext viewcontext, textwriter writer) +107    system.web.mvc.viewresultbase.executeresult(controllercontext context) +291    system.web.mvc.controlleractioninvoker.invokeactionresult(controllercontext controllercontext, actionresult actionresult) +13    system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist`1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +56    system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist`1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) +420    system.web.mvc.controlleractioninvoker.invokeactionresultwithfilters(controllercontext controllercontext, ilist`1 filters, actionresult actionresult) +52    system.web.mvc.async.<>c__displayclass2b.<begininvokeaction>b__1c() +173    system.web.mvc.async.<>c__displayclass21.<begininvokeaction>b__1e(iasyncresult asyncresult) +100    system.web.mvc.async.wrappedasyncresult`1.callenddelegate(iasyncresult asyncresult) +10    system.web.mvc.async.wrappedasyncresultbase`1.end() +49    system.web.mvc.async.asynccontrolleractioninvoker.endinvokeaction(iasyncresult asyncresult) +27    system.web.mvc.controller.<beginexecutecore>b__1d(iasyncresult asyncresult, executecorestate innerstate) +13    system.web.mvc.async.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult) +29    system.web.mvc.async.wrappedasyncresultbase`1.end() +49    system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) +36    system.web.mvc.controller.<beginexecute>b__15(iasyncresult asyncresult, controller controller) +12    system.web.mvc.async.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult) +22    system.web.mvc.async.wrappedasyncresultbase`1.end() +49    system.web.mvc.controller.endexecute(iasyncresult asyncresult) +26    system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult) +10    system.web.mvc.mvchandler.<beginprocessrequest>b__5(iasyncresult asyncresult, processrequeststate innerstate) +21    system.web.mvc.async.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult) +29    system.web.mvc.async.wrappedasyncresultbase`1.end() +49    system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) +28    system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult result) +9    system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +9765901    system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +155 

and questionscontroller :

    // get: questions     public actionresult index()     {         var questions = db.questions.include(q => q.step);         return view(questions.tolist().groupby(x=>x.idstep));     }      // get: questions/details/5     public actionresult details(int? id)     {         if (id == null)         {             return new httpstatuscoderesult(httpstatuscode.badrequest);         }         question question = db.questions.find(id);         if (question == null)         {             return httpnotfound();         }         return view(question);     }      // get: questions/create     public actionresult create()     {         viewbag.idstep = new selectlist(db.steps, "idstep", "namestep");         return view();     }      // post: questions/create     // protect overposting attacks, please enable specific properties want bind to,      // more details see http://go.microsoft.com/fwlink/?linkid=317598.     [httppost]     [validateantiforgerytoken]     public actionresult create([bind(include = "idquestion,idstep,question1")] question question)     {         if (modelstate.isvalid)         {             db.questions.add(question);             db.savechanges();             return redirecttoaction("index");         }          viewbag.idstep = new selectlist(db.steps, "idstep", "namestep", question.idstep);         return view(question);     }      // get: questions/edit/5     public actionresult edit(int? id)     {         if (id == null)         {             return new httpstatuscoderesult(httpstatuscode.badrequest);         }         question question = db.questions.find(id);         if (question == null)         {             return httpnotfound();         }         viewbag.idstep = new selectlist(db.steps, "idstep", "namestep", question.idstep);         return view(question);     }      // post: questions/edit/5     // protect overposting attacks, please enable specific properties want bind to,      // more details see http://go.microsoft.com/fwlink/?linkid=317598.     [httppost]     [validateantiforgerytoken]     public actionresult edit([bind(include = "idquestion,idstep,question1")] question question)     {         if (modelstate.isvalid)         {             db.entry(question).state = entitystate.modified;             db.savechanges();             return redirecttoaction("index");         }         viewbag.idstep = new selectlist(db.steps, "idstep", "namestep", question.idstep);         return view(question);     }      // get: questions/delete/5     public actionresult delete(int? id)     {         if (id == null)         {             return new httpstatuscoderesult(httpstatuscode.badrequest);         }         question question = db.questions.find(id);         if (question == null)         {             return httpnotfound();         }         return view(question);     }      // post: questions/delete/5     [httppost, actionname("delete")]     [validateantiforgerytoken]     public actionresult deleteconfirmed(int id)     {         question question = db.questions.find(id);         db.questions.remove(question);         db.savechanges();         return redirecttoaction("index");     }      protected override void dispose(bool disposing)     {         if (disposing)         {             db.dispose();         }         base.dispose(disposing);     } } } 

and view :

@model ienumerable<webapplication1.question>    @{      viewbag.title = "index";  }    <h2>index</h2>    <p>      @html.actionlink("create new", "create")  </p>  <table class="table">      <tr>          <th>            </th>          <th>              @html.displaynamefor(model => model.step.namestep)          </th>          <th>              @html.displaynamefor(model => model.question1)          </th>          <th></th>      </tr>    @foreach (var item in model) {      <tr>          <td>              @html.actionlink("edit", "edit", new { id = item.idquestion }) |              @html.actionlink("details", "details", new { id = item.idquestion }) |              @html.actionlink("delete", "delete", new { id = item.idquestion })          </td>          <td>              @html.displayfor(modelitem => item.step.namestep)          </td>          <td>              @html.displayfor(modelitem => item.question1)          </td>        </tr>  }    </table>

the problem originated how returning view page code:

return view(questions.tolist().groupby(x => x.idstep)); 

the groupby linq method produces groupedenumerable result (not ienumerable) , since model expects ienumerable collection given @model directive, there's no direct conversion groupedenumerable ienumerable unless using method returns ienumerable, i.e. tolist.

try using groupby before tolist given below, action method returns list<question> implements ienumerable:

public actionresult index() {     var questions = db.questions.include(q => q.step).groupby(x => x.idstep).tolist();     return view(questions); } 

nb: if you're not sure putting groupby directly after include, use asenumerable convert join query collection before start grouping.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -