简体   繁体   English

查看为 Controller 提供相同的值

[英]View providing the same value to Controller

I'm trying to write a simple numbers checking program.我正在尝试编写一个简单的数字检查程序。 You enter a correct next digit - it shows you next one, if not - nothing happens.您输入了正确的下一位数字 - 它会显示下一位,如果不是 - 没有任何反应。 In the Controller I'm trying to: set a value of variable, increment it, pass to specific View (howManyDigits).在 Controller 中,我试图:设置变量的值,递增它,传递给特定的视图(howManyDigits)。 I know that I can make howManyDigits a session variable but the point here is to understand why the number is still going back to eg.我知道我可以将 howManyDigits 设为 session 变量,但这里的重点是要了解为什么这个数字仍然会回到例如。 "2" which was entered in View1 just after the first run of the app.在应用程序第一次运行后在 View1 中输入的“2”。

PiController: Pi控制器:

namespace PI.Controllers
{
    public class PiController : Controller
    {
        [HttpGet]
        public ActionResult View1()
        {
            return View();
        }

        [HttpPost]
        public ActionResult View2(Pinumber number)
        {
            if (number.tabPi[number.howManyDigits] != number.numberEntered)
                number.howManyDigits++;

            return View(number);

        }

    }
}

View1:视图1:

@model PI.Models.Pinumber

@using (Html.BeginForm("View2", "Pi", FormMethod.Post))
{
    @Html.TextBoxFor(number => number.howManyDigits,new { autofocus = "autofocus"})

    <input type="submit" value="How many numbers do you know?" />
} 

In this view I have BeginForm and i'm trying to pass this variable using HiddenFor and Lambda expression back to the same Controller.在这个视图中,我有 BeginForm,我正在尝试使用 HiddenFor 和 Lambda 表达式将此变量传递回相同的 Controller。

View2:视图2:

@model PI.Models.Pinumber

<div>
    3.@for (int i = 0; i < Model.howManyDigits; i++)
    {
        @Model.tabPi[i]
    }
</div>

@using (Html.BeginForm("View2", "Pi", FormMethod.Post))
{
    @Html.HiddenFor(x=> x.howManyDigits)
    @Html.TextBoxFor(x => x.numberEntered, new { autofocus = "autofocus" })

}

Model class Pinumber: Model class 编号:

namespace PI.Models
{
    public class Pinumber
    {
        public char[] tabPi { get; set; } = new char[100000];
        public Pinumber()
        {
                for (int i = 0; i < 20; i++)
                {
                    tabPi[i] = piNumber[i];
                }
        }
        public int howManyDigits { get; set; }
        public char numberEntered { get; set; }

        public string piNumber = "141592653589793238462"

Unfortunately it's going back to Controller with an old value which i entered in other View1 which is used just to display the start site with entry number of digits that you already know - [HttpGet].不幸的是,它会回到 Controller 并带有我在其他 View1 中输入的旧值,该值仅用于显示带有您已经知道的输入位数的起始站点 - [HttpGet]。

Things are not working that way.事情不是这样的。

Look, if you increment an value of some int variable, from 1 to 2 let's say, and you pass it to view.看,如果你增加一些 int 变量的值,比如说从 1 到 2,然后你将它传递给视图。 Then you want to increment it from 2 to 3, you need to send that 2 from View(from HTML code) back to the backend, and increment it there, otherwise, value is lost.然后你想把它从 2 增加到 3,你需要把那个 2 从 View(从 HTML 代码)发送回后端,然后在那里增加它,否则,价值会丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM