使用MVC访问从另一个视图传递的数据时遇到问题

时间:2015-06-04 18:19:03

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

我试图使用MVC将数据从一个视图传递到另一个视图。我正在尝试做一个简单的猜谜游戏,在第一个视图中,我们输入一系列数字,然后在第二个视图中我们尝试猜测数字。我能够将值存储在我的模型中,但是我无法在另一个视图的js脚本中访问它们/使用它们。很抱歉,如果代码太多,MVC很难在没有显示全部代码的情况下寻求帮助。当我转到第二个视图来猜测数字时,它无法识别@ViewBag.(model => model.Low)并且显示Load is not defined

模型

pubblic class Range
{
    public int High
    {
        get
        {
            if (HttpContext.Current.Session["High"] == null)
            {
                HttpContext.Current.Session["High"] = 3;
            }

            return (int)HttpContext.Current.Session["High"];

        }
        set
        {
            HttpContext.Current.Session["High"] = value;
        }
    }


    public int Low
    {
        get
        {
            if (HttpContext.Current.Session["Low"] == null)
            {
                HttpContext.Current.Session["Low"] = 1;
            }

            return (int)HttpContext.Current.Session["Low"];

        }
        set
        {
            HttpContext.Current.Session["Low"] = value;
        }
    }
}

控制器

public class GuessingGameController : Controller
{

    public ActionResult EnterRange()
    {
        return View();
    }

    [HttpPost]
    public ActionResult EnterRange(Range range)
    {
        int high = range.High;
        int low = range.Low;
        return View(range);
    }

    public ActionResult GuessNumber()
    {
        return View();
    }

}

查看1:输入范围

@model GameMVC.Models.Range

@using (Html.BeginForm("EnterRange", "GuessingGame"))
{
    <center>
        <h2>Lets play a game.</h2>

        Enter A Range of Numbers:
        <br />
        Low: @Html.TextBoxFor(m => m.Low)
        <br />
        High: @Html.TextBoxFor(m => m.High)
        <br />
        <input type="submit" value="Enter"/>
        <p>
            @Html.ActionLink("Now, To the Game", "GuessNumber", "GuessingGame")
        </p>
    </center>
}

查看2:猜数

@model GameMVC.Models.Range
<script language="JavaScript">
    var myNum, count;

    function Load() {
        document.game.status.value = "Please set range of numbers and press the Start button.";
        document.game.number.focus();
    }

    function Round(scale) {

        var dd = new Date();
        return((Math.round(Math.abs(Math.sin(dd.getTime())) * 8.71 * scale) % scale));
    }

    function myRange() {
        var to = 1 + 1 * @ViewBag.(model => model.Low);
        count = 0;
        myNum = Round(to);
        while (myNum < @ViewBag.(model => model.High);)
            myNum = Round(to);
        document.game.status.value = "Please guess a number, enter it, and press Guess.";
    }

    function Guess() {
        var numberGuess = document.game.number.value;
        count++;
        if (numberGuess < myNum) alert("My number is greater than " + numberGuess + ".");
        else if (numberGuess > myNum) alert("My number is less than " + numberGuess + ".");
        else alert("It takes you " + count + " attempts to guess this number");
    }
</script>

<body onload=" Load() ">
<div style="text-align: center;">
    <form name=game>
        Guess: <input type="text" name="number" size=10>
        <p>
            <br/>
            <input type="button" value="Guess" onclick=" Guess() ">
        </p>

        @Html.Label("status")
    </form>
</div>
</body>

2 个答案:

答案 0 :(得分:0)

您可以尝试使用@ViewBag.(model => model.Low)

,而不是@Model.Low

答案 1 :(得分:0)

“GuessAction”视图未提供模型实例。您需要将“Range”的实例传递给“GuessNumber”视图。由于您正在从会话中访问值,因此您只需传递一个新实例。

WifiP2pManager

然后在javascript中,您可以简单地以@ Model.High和@ Model.Low

的形式访问数据

如果只需要一个请求,也可以使用TempData存储高值和低值。