2014-05-04 85 views
0

我在傳遞變量分數時遇到了一些問題,這是我的評估類中的一個變量,在我的[httpPost] ifstatements(評估模型)方法在我的Quiz控制器中屬於不同類的不同控制器的視圖。當分數大於零時,即我回答至少有一個問題是正確的,我將被帶到結果視圖,並向我顯示分數。但是打印時遇到問題即時消息,在被稱爲指數另一種觀點認爲這是我的索引方法的一部分,在LEADERBOARD類的我LEADERBOARD控制器將變量從一個類傳遞到不同類的視圖

測驗控制器

public ActionResult ifStatement() 
{ 
    var evalVM = new Evaluation(); 

    //the below is hardcoded for DEMO. you may get the data from some 
    //other place and set the questions and answers 

    var q1 = new Question { ID = 1, QuestionText = "1. Which of the following shows the correct syntax for an if statement?" }; 
    q1.Answers.Add(new Answer { ID = 12, AnswerText = "a. if expression" }); 
    q1.Answers.Add(new Answer { ID = 13, AnswerText = "b. if { expression }" }); 
    q1.Answers.Add(new Answer { ID = 14, AnswerText = "c. if (expression){}" }); 
    q1.Answers.Add(new Answer { ID = 15, AnswerText = "d. expression if" }); 
    evalVM.Questions.Add(q1); 

    return View(evalVM); 
} 

[HttpPost] 
public ActionResult ifStatement(Evaluation model) 
{ 
    int score = 0; 

    if (ModelState.IsValid) 
    { 
     using (UsersContext db = new UsersContext()) 
     { 
      foreach (var q in model.Questions) 
      { 
       var qId = q.ID; 

       List<string> listSelectedAnswer = new List<string>(); 

       listSelectedAnswer.Add("14"); 
       listSelectedAnswer.Add("19"); 
       listSelectedAnswer.Add("22"); 
       listSelectedAnswer.Add("25"); 
       listSelectedAnswer.Add("28"); 
       listSelectedAnswer.Add("33"); 

       foreach (string answer in listSelectedAnswer) 
       { 
        foreach (var answer2 in model.Questions) 
        { 
         if (answer == answer2.SelectedAnswer) 
         { 
          score = score + 3; 
         } 
        } 
       } 

       model.Score = score; 

       ViewBag.score = model.Score; 

       if (score > 0) 
       { 
        return View("results"); 
       } 
       else 
       { 
        //score = 0; 
        //return Content("Please check all answers have been submitted!"); 
        return Content("Error, please ensure all questions have been answered. You may use the back button to continue the Quiz."); //PRG Pattern 
       } 
      } 
     } 
    } 

    model.setScore(score); 
    return View("ThankYou"); 
} 

排行榜控制器

比分淘汰
public class LeaderboardController : Controller 
{ 
    UsersContext db = new UsersContext(); 
    // 
    // GET: /Leaderboard/ 

    TutorialEntities t = new TutorialEntities(); 
    Evaluation e = new Evaluation(); 

    public ActionResult Index() 
    { 
     // Evaluation ev = new Evaluation(); 
     // var tutorial = t.Evaluations.ToList(); 
     //ViewBag.Scores = t.Evaluations.Select(a => a.Score).ToList(); 
     //ViewBag.Scores = e.getScore(); 
     //ViewBag.Score = e.getScore(); 

     ViewBag.Users = db.UserProfiles.ToList(); 
     ViewBag.Scores = t.Evaluations.ToList(); 
     return View(); 

    } 
} 

索引視圖

<h2>Leaderboard</h2> 

@foreach (var item2 in ViewBag.Users) 
{ 
    <p> 
     Score: @ViewBag.Score 
    </p> 
} 

@foreach (var item in ViewBag.Users) 
{ 
    <p> 
     UserID: @item.UserId 
     <br /> 
     UserName: @item.UserName 
     <br /> 
    </p> 
} 

當我去這個視圖時,頁面上顯示的所有內容都是在我的網站和他們的ID上註冊的人的名字,但分數是空白的!我似乎無法得到我的頭,我已經嘗試了很多方法,但它似乎不能看到正在初始化的分數ifstatement方法

回答

0

您的控制器設置viewbag.scores,但您的視圖試圖訪問viewbag。得分了?

+0

感謝您的答覆,我改成了這個 @foreach(VAR ITEM2在ViewBag.Score) {

得分:@ ViewBag.Score

}在ViewBag @foreach(VAR項目。用戶) {

用戶名:item.UserId
用戶名:item.UserName

} 但仍然顯示在視圖中的所有內容是userId和USerName,即使是實際的單詞「Score」也不顯示 – user3601983

相關問題