我遇到了问题,或者也许我不明白这应该如何工作:)如果有帮助的话。因此,我有一个空白的asp.net核心Web项目,其中将我的问题汇总在一起,请参见下面的代码。如果我单击Submit1按钮,它将调用OnPostSubmit1并将值放入MyTestDetails List变量以及测试程序字符串变量,然后返回页面。如果我然后单击Submit2按钮,它将调用OnPostSubmit2,如果我那时调试,测试器仍然具有OnPostSubmit1先前调用中的“这是我的测试”的先前值,但是MyTestDetails List变量被重置并且丢失了我在其中设置的值当我单击Submit1按钮并调用OnPostSubmit1时。只是想弄清楚我如何让MyTestDetails坚持下去?就像字符串测试器一样,我将其设置为“ public”。
Razor Page .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 />
Razor Page .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; }
}