2017-07-26 19 views
0

I get the following error when trying to run my application:モデル項目は型である「System.Linq.GroupedEnumerable`3が、これは必要」System.Collections.Generic.IEnumerable`1を辞書に渡さモデル項目がである

'System.Lineq.GroupedEnumerable'3 [WebApplication1.Question、System.Int32、WebApplication1.Question]'と入力しますが、この辞書には 'System.Collections.Generic.IEnumerable`1 [WebApplication1.Question]というタイプのモデルアイテムが必要です。ここで

スタックトレースです:

[InvalidOperationException: The model item passed into the dictionary is of type 'System.Linq.GroupedEnumerable`3[WebApplication1.Question,System.Int32,WebApplication1.Question]', but this dictionary requires a 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 

、私の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 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // 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 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // 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); 
    } 
} 
} 

そしてマイビュー:

@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>

答えて

0

問題は、このコードでビュー・ページを返す方法をあなた由来:

return View(questions.ToList().GroupBy(x => x.idStep)); 

GroupBy LINQメソッドは、その結果(ないIEnumerable)としてGroupedEnumerableを生成し、あなたのモデルがIEnumerableコレクションを見込んでいるため@modelディレクティブで与えられますIEnumerable、すなわちToListを返すメソッドを使用しない限り、GroupedEnumerableからIEnumerableへの直接的な変換はありません。

下記の通りごアクションメソッドがIEnumerableを実装List<Question>を返すように、ToListGroupByを使用してみてください:

public ActionResult Index() 
{ 
    var questions = db.Questions.Include(q => q.Step).GroupBy(x => x.idStep).ToList(); 
    return View(questions); 
} 

NB:あなたはInclude後に直接GroupByを置くわからない場合は、参加変換することAsEnumerableを使用グループ化を開始する前にコレクションに照会します。

+0

ありがとうございました。しかし、私は各ステップの質問のリストを表示したい。 –

+0

ここに表示したい "各ステップ"について記述できますか?私はあなたが 'OrderBy'などを使ってユーザーがどのようなステップで質問を分ける必要があると思います。 –

+0

私はこれを持っています:ステップ番号1 |質問1 ステップ番号1 |質問2 ステップ番号1 |質問3 ステップ番号2 |質問4 ステップ番号2 |質問5 ステップ番号2 |質問6 –

関連する問題