2016-04-01 14 views
0

Rastgele bir öğrenci seçip rastgele bir soru seçip bir nedenle neden .cshtml dosyasında çalışamıyorum bir ASP.NET MVC programı oluşturma konusunda sorun yaşıyor. Yanlış bir şey mi yapıyorum? Ayrıca ben bu hataASP.Net MVC rasgele, Denetleyiciler ve görünümler

olsun "sözlüğüne geçirilen modeli madde türü 'QuizProgramMVC.Models.Student' arasında, ancak bu sözlüğü türü 'QuizProgramMVC.Models.StudentQuestion' modeli öğe gerektirir." `Kullanılarak QuizProgramMVC.Models;

Kontrolör sorun farklı model ile aynı görünüm sayfasını aradığınız edilir

@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

Hata mesajı açık değil mi? Eylem yönteminizden farklı bir türü beklenenden daha geçiyorsunuz. Öğrenci nesnesi yerine, görünümünüz güçlü bir şekilde yazıldığından 'StudentQuestion' nesnesinin bir nesnesini geçirmeniz gerekir. – Shyju

cevap

0

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); 
    } 

} 

Görünüm. QuizProgram() o görünüm sayfasında size

@model QuizProgramMVC.Models.StudentQuestion 

olarak modelini tanımlanan çünkü bir hata atar O bunun nedeni diğer ViewModels izin vermez aracılığıyla üzerindeki Görünüm Page (.cshtml) çağrılması

public ActionResult QuizProgram() 
{   
    Student program = new Student();//Here you need to pass the StudentQuestion to the view. 
    return View(program); 
} 

güçlü yazılmıştır Görünüm.

İlgili konular