2014-01-06 119 views
0

剛剛接觸c#我無法弄清楚我遇到的問題。我創建了一個視圖,仍然是,我的模型是空的,可能是什麼原因造成的?爲什麼我的模型爲空?

下面是我的模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Prigmore2013_01.Models 
{ 
    public class GuessingGame 
    { 
     private Random _random; 

     public GuessingGame() 
     { 
      this._random = new Random(); 
      this.Guesses = new List<Guess>(); 
      this.Target = new List<int>(){ 1, 2, 3 }; 
     } 

     public List<int> Target { get; set; } 
     public List<Guess> Guesses { get; set; } 

     public List<Guess> ShowGuessesMade() 
     { 
      return Guesses; 
     } 
     public void NewGame() 
     { 
      this.Target.Clear(); 
      var count = 4; 
      for (var i = 1; i < count; i++) 
      { 
       var swap = _random.Next(1, 9); 

       if (!this.Target.Contains(swap)) 
       { 
        this.Target.Add(swap); 
       } 
      } 
     } 
     public void GuessTheHiddenDigits(List<int> guesses) 
     { 
      Guess g = new Guess() { Digits = guesses }; 
      //compare the lists 
      var list = this.Target; 
      var list2 = g.Digits; 

      for (int i = 0; i < list.Count; i++) 
      { 
       if (list[i] == list2[i]) 
       { 
        g.RightDigitRightPosition++; 
       } 
      } 
      //Now calculate how many digits in guesses are just plain wrong 
      List<int> result = g.Digits.Except(this.Target).ToList(); 
      g.RightDigitWrongPosition = g.Digits.Count - result.Count - g.RightDigitRightPosition; 

      //handle duplicates 
      if (list.Count != list2.Distinct().Count()) 
      { 
       // set thet right digit wrong position 
       for (int i = 0; i < list2.Distinct().Count(); i++) 
       { 
        g.RightDigitWrongPosition = i; 
       } 
      } 
      this.Guesses.Add(g); 
     } 
    } 
} 

查看:

@{ 
    ViewBag.Title = "Index"; 
    @model Prigmore2013_01.Models.GuessingGame 
} 
<h2>Guessing The Digits</h2> 
<p>Insert your 3 guesses into the textboxes below</p> 
<form action="Exercise09/GuessTheDigits" method="post"> 
    <label for="guesses">Guess 1: @Html.TextBoxFor(x => x.Guesses[0], new { style = "width:12px", MaxLength = "1" }) Guess 2: @Html.TextBoxFor(x => x.Guesses[1], new { style = "width:12px", MaxLength = "1" }) Guess 3: @Html.TextBoxFor(x => x.Guesses[2], new { style = "width:12px", MaxLength = "1" })</label> 
    <br /> 

    <label for="pastGuesses">Your Previous Guesses</label> 
    <table> 
     <tr> 
      <td>Your Guess</td> 
      <td>Spot On</td> 
      <td>Near Miss</td> 
     </tr> 
     <!-- Need to loop here --> 
     @if (Model != null) 
     { 
      for (var i = 0; i < Model.Guesses.Count(); i++) 
      { 
       foreach (var item in Model.Guesses) 
       { 
      <tr> 
       <td>@Html.DisplayFor(x => item.Digits)</td> 
       <td>@Html.DisplayFor(x => item.RightDigitRightPosition)</td> 
       <td>@Html.DisplayFor(x => item.RightDigitWrongPosition)</td> 
      </tr> 
       } 
      } 
     } 
     else 
     { 
      <p>Fail - Model is null</p> 
     } 
    </table> 

    <button type="submit" name="Submit">Submit</button> 
</form> 
<form action="Exercise09/StartNewGame" method="post"> 
    <button type="submit" name="Submit">New Game</button> 
</form> 

控制器:

using Prigmore2013_01.Models; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace Prigmore2013_01.Tests 
{ 
    public class Exercise09Controller : Controller 
    { 
     // 
     // GET: /Exercise09/ 

     public ViewResult Index() 
     { 
      return View("Index"); 
     } 



     public ViewResult ShowPreviousGuesses() 
     { 
      if (HttpContext.Session["GameState"] == null) 
      { 
       HttpContext.Session["GameState"] = new GuessingGame(); 
      } 

      GuessingGame theGame = this.Session["GameState"] as GuessingGame; 
      return View("Index", theGame.Guesses); 
     } 




     public ViewResult ShowGuessesMade() 
     { 
      return View(); 
     } 



     public ActionResult GuessTheDigits(List<int> guesses) 
     { 
      if (HttpContext.Session["GameState"] == null) 
      { 
       HttpContext.Session["GameState"] = new GuessingGame(); 
      } 

      GuessingGame theGame = this.Session["GameState"] as GuessingGame; 

      theGame.GuessTheHiddenDigits(guesses); 

      return RedirectToAction("Index", theGame.Guesses); 
     } 




     public RedirectToRouteResult StartNewGame() 
     { 
      GuessingGame theGame = this.Session["GameState"] as GuessingGame; 
      theGame.Target.Clear(); 
      var rand = new Random(); 

      for (int i = 0; i < 4; i++) 
      { 
       if (!theGame.Target.Contains(rand.Next(1, 10))) 
       { 
        theGame.Target.Add(rand.Next(1, 10)); 
       } 
      } 


      return RedirectToRoute(new 
      { 
       controller = "Exercise09", 
       action = "Index" 
      }); 
     } 
    } 
} 

我覺得我一起將R是ight lines但不明白是什麼導致了這個問題?有人能夠將我鏈接到適當的閱讀材料,以便進一步瞭解,以便我可以在將來防止這種情況發生。

回答

2

你的觀點是期待:

@model Prigmore2013_01.Models.GuessingGame 

但是你逝去:

return View("Index", theGame.Guesses); 

要麼改變你的看法,以期望List<Prigmore2013_01.Models.Guess>

@model List<Prigmore2013_01.Models.Guess> 

或更改控制器發送a Prigmore2013_01.Models.GuessingGame查看:

return View("Index", theGame); 

根據您的代碼,更改控制器方法的返回類型以返回猜測遊戲對象似乎更直觀。

1

您的模特兒是GuessingGame,您即將通過List<Guess>。在您看來它更改爲:

@model List<Prigmore2013_01.Models.Guess> 

而且,你不傳遞任何作爲模型視圖您Index操作方法,這樣也將導致它是null。底線,您必須更改視圖以接受正確的類型或更改您的控制器操作方法以傳遞正確的類型。