2016-04-01 17 views
0

ランダムに学生を選択してランダムに質問を選択するASP.NET MVCプログラムを作成する際に問題が発生しました。何らかの理由で.cshtmlファイルで動作しません。私は何か間違っているのですか?また、私はこのエラーASP.Net MVCランダム、コントローラとビュー

を得る「辞書に渡されるモデルアイテムはタイプ 『QuizProgramMVC.Models.Student』であるが、この辞書は、型 『QuizProgramMVC.Models.StudentQuestion』のモデルアイテムが必要です。」 `使用しますQuizProgramMVC.Models;

コントローラ

public class QuizController : Controller 
{ 
    public QuizController TheQuiz { get; set; } 

    // GET: Quiz 
    public ActionResult Index() 
    { 
     StudentQuestion sq = new StudentQuestion(); 

     return View(sq); 
    } 

    public ActionResult QuizProgram() 
    {   
     Student program = new Student(); 

     return View(program); 
    } 

} 

ビュー

@model QuizProgramMVC.Models.StudentQuestion 
@using QuizProgramMVC.Models; 

@{ 

    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>QuizProgram</title> 
</head> 
<body> 
    <div> 
     <h1>Quiz Program</h1> 

       <div class="row"> 
        <div class="col-md-6"> 
         <table class="table table-condensed"> 
          <thead> 
           <tr> 
            <th>Question</th> 
            <th>Answer</th> 
           </tr> 
          </thead> 
          <tbody> 
           @foreach (Question q in Model.Questions) 
           { 
            <tr> 
             <td> 
              @q.Quest 
             </td> 
             <td> 
              @q.Answer 
             </td> 
            </tr> 
           } 
          </tbody> 
         </table> 
        </div> 
       </div> 
      <br/> 
       <div class="row"> 
        <div class="col-md-6"> 
         <table class="table table-condensed"> 
          <thead> 
           <tr> 
            <th>First Name</th> 
            <th>Last Name</th> 
           </tr> 
          </thead> 
          <tbody> 
           @foreach (Student s in Model.Students) 
           { 
            <tr> 
             <td> 
              @s.FirstName 
             </td> 
             <td> 
              @s.LastName 
             </td> 
            </tr> 
           } 
          </tbody> 
         </table> 
        </div> 
       </div> 
     <br/> 
     @using (Html.BeginForm()) 
     { 
      <div> 
       Type Answer: <input id="param1" name="param1" type="text" /> 
       <br /> 
       <input type="submit" value="Submit" /> 
      </div> 
     } 
    </div> 
</body> 
</html> 
+2

エラーメッセージは明白ではありませんか?アクションメソッドから期待したものとは異なるタイプを渡しています。 Studentオブジェクトの代わりに、あなたのビューが強く型付けされているので、 'StudentQuestion'のオブジェクトを渡す必要があります。 – Shyju

答えて

0

問題は、別のモデルと同じビューのページを呼び出しているです。そのビューページであなたはそれはそれなぜなら他のviewmodelsを許可していません

@model QuizProgramMVC.Models.StudentQuestion 

としてモデルを定義しているため

QuizProgram(経由上記表示ページ(.cshtml)を呼び出す
public ActionResult QuizProgram() 
{   
    Student program = new Student();//Here you need to pass the StudentQuestion to the view. 
    return View(program); 
} 

でエラーが発生しますは厳密にはです。

関連する問題