Razor 页面当我从 post 方法调用它时,我丢失了从用户页面传递的用户数据

Razor page I lost the User data Passed from the user page when I call it from the post method

因为我需要在具有许多不同模型和视图的许多不同表中创建我的用户信息,所以我在地址页面中使用了这段代码,但正如我的评论所示,我在 post 函数中丢失了用户信息我的问题是为什么和做什么?????顺便说一下,当我将 User1 difenation 从我的 OnGet 函数复制到 OnPost 函数时,这段代码工作得很好,正如我在评论中所解释的那样,但我仍然想了解为什么 public 属性 会丢失信息,请阅读我的评论

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using RazorPagesUI.Models;
namespace RazorPagesUI.Pages.Forms
{
    partial class AddAddressModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        public AddAddressModel(ILogger<IndexModel> logger)
            {
                _logger = logger;
            }
            [BindProperty(SupportsGet = true)]
            public string Mail { get; set; }
            public IEnumerable<SelectListItem>? Country { get; set; }
            
            [BindProperty]
            public AddressModel? Address { get; set; }
            public string SelectedString { get; set; }
            
            public UserModel User1 { get; set; }=new UserModel();
            public void OnGet()
            {
                List<string> TagIds = Mail.Split(',').ToList();
                Int32.TryParse(TagIds[0], out int j);
                User1.Id = j;
                User1.Email = TagIds[1];
                User1.FirstName = TagIds[2];
                User1.LastName = TagIds[3];
                User1.Password = TagIds[4]
                   
                Country = new SelectListItem[]
                {
                    new SelectListItem ("Canada", "Canada"),
                    new SelectListItem ("Egypt", "Egypt"),
                    new SelectListItem ( "Usa", "Usa")
                };
            }
            public IActionResult OnPost()
            {
                //when I get to here User1 is null 
                Address.Country = Request.Form["country"];
                if (ModelState.IsValid == false)
                {
                    return Page();
                }
             //I need to insert my user info to my user table but User1 is null 
             //here I insert Address info 
                return RedirectToPage("/index", new{ Name = User1.Firstname);//User1 
               becomes Null
            }
        }
     }

cshtml 文件 按照要求包含在我的 post

@page 
@using RazorPagesUI.Models
@model RazorPagesUI.Pages.Forms.AddAddressModel
@{
    ViewData["Title"] = "Add Address";
}
<b>Adderres for: @Model.User1.FirstName @Model.User1.LastName</b>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
<div class="text-center">
    <h1>Add Address</h1>
</div>
<form method="post">
    <div class="container-fluid">
        <div class="p-1">
            <div class="text-center">
                <select name = "country" asp-items="@Model.Country"> 
    </select> 
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.State" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.City" 
    />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.StreetNumber" 
    placeholder="Street #" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.StreetName" 
     placeholder="Street Name" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.AppNumber" 
  placeholder="App#" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.ZipCode" />
            </div>
        </div>
        <div class="p-1">
            <div class="text-center">
                <input type="tel" asp-for="Address.Phone"  />
            </div>
        </div>
        <div class="p-1">
            <div class="text-center">
                <input type="tel" asp-for="Address.CellPhone" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <button type="submit">Submit</button>
            </div>
        </div>
    </div>
</form>

您必须显示您的 cshtml 文件,即 Razor 页面的前端,以便更清楚地描述您的问题。但总的来说,我看到您正在尝试绑定一个名为 Country 的 属性 复杂对象的 Address 类型 AddressModel 在这种情况下,您的 cshtml 文件中的 input/select 应该反映 Address 对象的目标 Country 属性 的复杂路径。它应该是这样的 <select name="Address.Country" asp-items="Model.Country"></select> 注意 select 元素的名称 Address.Country 即它反映了目标 属性 的完整路径。有关 razor 页面中复杂模型绑定的更多信息,请参见此处 https://www.learnrazorpages.com/razor-pages/model-binding 如果您设法正确绑定复杂对象的 属性,则此行代码 Address.Country = Request.Form["country"]; 变得多余。 Address.Country 的值应该自动填充。

首先,你需要在表单post时传递User1.FirstName,这样你就可以在OnPost处理程序中得到User1.FirstName

表单(添加隐藏输入User1.FirstName):

<form method="post">
    <div class="container-fluid">
        <div class="p-1">
            <div class="text-center">
                <select name = "country" asp-items="@Model.Country"> 
    </select> 
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.State" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.City" 
    />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.StreetNumber" 
    placeholder="Street #" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.StreetName" 
     placeholder="Street Name" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.AppNumber" 
  placeholder="App#" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="text" asp-for="Address.ZipCode" />
            </div>
        </div>
        <div class="p-1">
            <div class="text-center">
                <input type="tel" asp-for="Address.Phone"  />
            </div>
        </div>
        <div class="p-1">
            <div class="text-center">
                <input type="tel" asp-for="Address.CellPhone" />
            </div>
        </div>
        <div class="text-center">
            <div class="p-1">
                <input type="hidden" asp-for="User1.FirstName" />
                <button type="submit">Submit</button>
            </div>
        </div>
    </div>
</form>

cshtml.cs(如果要绑定数据到User1,需要使用[BindProperty],这样OnPost handler中就可以使用User1.Firstname):

 [BindProperty]
            public UserModel User1 { get; set; } = new UserModel();