【问题标题】:asp.net core razor page model public list variable resets to null onpost while string variable does notasp.net core razor 页面模型公共列表变量在发布后重置为空,而字符串变量不重置
【发布时间】:2020-09-18 00:10:22
【问题描述】:

我遇到了问题,或者我不明白这应该如何工作:) 如果有任何帮助表示赞赏。所以我有一个空白的 asp.net 核心 web 项目,我把我的问题放在一起,见下面的代码。如果我单击 Submit1 按钮,它会调用 OnPostSubmit1 并将值放入 M​​yTestDetails List 变量以及测试器字符串变量,然后返回页面。如果我然后单击 Submit2 按钮,它会调用 OnPostSubmit2,如果我在那时调试测试仪仍然具有来自 OnPostSubmit1 先前调用的“这是我的测试”的先前值,但 MyTestDetails List 变量被重置并丢失了我设置的值当我单击 Submit1 按钮并调用 OnPostSubmit1 时,它之前。只是想弄清楚如何让 MyTestDetails 坚持下去?我把它设置为“public”,就像字符串测试器一样。

剃刀页面 .cshtml

div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<form method="post">

    <button asp-page-handler="Submit1" type="submit" name="Submit1" class="btn btn-secondary"><i class="far fa-hand-point-right"></i>Submit1</button>
    
    <input hidden="hidden" asp-for="@Model.tester" value="@Model.tester" class="form-control" />
    <input hidden="hidden" asp-for="@Model.MyTestDetails" value="@Model.MyTestDetails" class="form-control" />

    <button asp-page-handler="Submit2" type="submit" name="Submit2" class="btn btn-secondary"><i class="far fa-hand-point-right"></i>Submit2</button>

</form>
<br />
<br />

剃刀页面 .cs

 private readonly ILogger<IndexModel> _logger;
        public string PopupMessage { get; set; }
        public List<TestDetails> MyTestDetails { get; set; }
        public string tester { get; set; }

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }

        public void OnPostSubmit1()
        {

           
            MyTestDetails = new List<TestDetails>();
            TestDetails testdata = new TestDetails();
            testdata.test1 = "testing1";
            testdata.test2 = "testing2";
            MyTestDetails.Add(testdata);

            tester = "this is my test";

        }

        public void OnPostSubmit2()
        {
            foreach (TestDetails test in MyTestDetails)
            {
                string testing = test.test1;
            }

            tester = "this is my test 2";

        }
    }

    public class TestDetails
    {
        public string test1 { get; set; }
        public string test2 { get; set; }

    }

【问题讨论】:

  • 如果我的回答帮助你解决了你的问题,请采纳为答案,这样可以帮助其他有同样问题的人更容易找到答案。

标签: asp.net asp.net-core


【解决方案1】:

只是想弄清楚如何让 MyTestDetails 坚持下去?

你需要了解的最重要的一点是输入隐藏控件不能直接存储列表对象的值,这就是为什么你无法获取MyTestDetails的内容OnPostSubmit2.

为了解决这个问题,可以通过循环Model.MyTestDetails获取MyTestDetails中的每组数据,并在每个循环中创建两个输入隐藏控件来存储test1test2 MyTestDetails 下的字段值。

这是页面中的完整代码:

    @page
    @model WebApplication1_rzaor_page.IndexModel
    @{
        ViewData["Title"] = "Index";
        Layout = "~/Pages/Shared/_Layout.cshtml"; 
    }
    
    
<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<form method="post">

    <button asp-page-handler="Submit1" type="submit" name="Submit1" class="btn btn-secondary"><i class="far fa-hand-point-right"></i>Submit1</button>

    <input hidden="hidden" asp-for="@Model.tester" value="@Model.tester" class="form-control" />

    @if (Model.MyTestDetails != null)
    {
        @for (int i = 0; i < Model.MyTestDetails.Count; i++)
        {
            <input hidden="hidden" asp-for="@Model.MyTestDetails[i].test1" value="@Model.MyTestDetails[i].test1" class="form-control" />
            <input hidden="hidden" asp-for="@Model.MyTestDetails[i].test2" value="@Model.MyTestDetails[i].test2"  class="form-control" />
        }
     
    }
    <button asp-page-handler="Submit2" type="submit" name="Submit2" class="btn btn-secondary"><i class="far fa-hand-point-right"></i>Submit2</button>

</form>
<br />
<br />

这是测试结果:

【讨论】:

  • 太棒了!感谢您的帮助并提供了很好的答复。我现在明白了。
猜你喜欢
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
相关资源
最近更新 更多