为什么我的模型为空?

时间:2014-01-06 18:33:12

标签: c# asp.net-mvc asp.net-mvc-4

对c#不熟悉我无法弄清楚我遇到的问题。我创建了一个视图,但我的模型仍然是null,可能是什么导致了这个?

以下是我的模特:

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

我认为我沿着正确的路线但是无法理解导致这个问题的原因是什么?是否有人能够将我与适当的阅读材料联系起来以获得进一步的理解,以便我将来可以防止这种情况发生。

2 个答案:

答案 0 :(得分:2)

您的观点期待:

@model Prigmore2013_01.Models.GuessingGame

但你传递的是:

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

将视图更改为List<Prigmore2013_01.Models.Guess>

@model List<Prigmore2013_01.Models.Guess>

或者更改您的控制器以向视图发送Prigmore2013_01.Models.GuessingGame

return View("Index", theGame);

根据您的代码,更改控制器方法的返回类型以返回GuessingGame对象似乎更直观。

答案 1 :(得分:1)

您的模型为GuessingGame,而您传递的是List<Guess>。在您的视图中将其更改为:

@model List<Prigmore2013_01.Models.Guess>

此外,在您的Index操作方法中,您没有将任何模型作为模型传递给视图,因此也会导致它null

最重要的是,您必须更改视图以接受正确的类型或更改控制器操作方法以传递正确的类型。