简体   繁体   English

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

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

because I need to create my user info in many different tables with many different Models and views I used this code in the address page but as shown in my remarks I lost the user info in the post function My question is why and what to do?????因为我需要在具有许多不同模型和视图的许多不同表中创建我的用户信息,所以我在地址页面中使用了此代码,但如我的评论所示,我在 function 帖子中丢失了用户信息我的问题是为什么以及该怎么做? ??? by the way when I copied the User1 difenation from my OnGet function to the OnPost function this code work perfectly as explained in my comment but I still want to understand why a public property lose the information please read my comments顺便说一句,当我将 User1 差异从我的 OnGet function 复制到 OnPost function 时,此代码可以完美地按照我的评论中的说明工作,但我仍然想了解为什么公共财产会丢失信息,请阅读我的评论

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 file As asked to include in my post cshtml 文件按要求包含在我的帖子中

@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>

You have to show your cshtml file ie the front end of the Razor page for a more clear description of your problem.您必须显示您的 cshtml 文件,即 Razor 页面的前端,以便更清楚地描述您的问题。 But in general, I'm seeing that you are trying to bind a property called Country of a complex object called Address of type AddressModel In this case the name of the input/select in your cshtml file should reflect the complex path to the target Country property of the Address object.但总的来说,我看到您正在尝试绑定一个名为Country的复杂 object 的属性,称为AddressModel类型的Address在这种情况下,您的 cshtml 文件中的输入/选择的名称应该反映到目标Country的复杂路径Address object 的属性。 It should be something like this <select name="Address.Country" asp-items="Model.Country"></select> Notice the name of the select element Address.Country ie it reflects the full path to the target property.它应该是这样的<select name="Address.Country" asp-items="Model.Country"></select>注意select元素Address.Country的名称,即它反映了目标属性的完整路径。 More information on complex model binding in razor pages here https://www.learnrazorpages.com/razor-pages/model-binding If you manage to bind the property of the complex object correctly this line of code Address.Country = Request.Form["country"]; More information on complex model binding in razor pages here https://www.learnrazorpages.com/razor-pages/model-binding If you manage to bind the property of the complex object correctly this line of code Address.Country = Request.Form["country"]; becomes redundant.变得多余。 The value of Address.Country should be populated automatically. Address.Country的值应自动填充。

Firstly,you need to pass User1.FirstName when form post,so that you can get User1.FirstName in OnPost handler.首先,您需要在表单发布时传递User1.FirstName ,以便您可以在 OnPost 处理程序中获取User1.FirstName

form(add hidden input with 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(If you want to bind the data to User1,you need to use [BindProperty] ,so that you can use User1.Firstname in OnPost handler): cshtml.cs(如果要将数据绑定到 User1,则需要使用[BindProperty] ,以便在 OnPost 处理程序中使用User1.Firstname ):

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

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

相关问题 数据未从 Razor 页面传递 - Data not being passed from Razor page 从内容页面调用母版页中的用户控制方法 - Call User Control Method in Master Page from Content Page 如何从用户控件调用方法到aspx页面? - How to call a method from user control to aspx page? 从aspx页面调用与用户控件绑定的Javascript方法 - Call Javascript method that is tied to user control from aspx page 从用户控件调用父页面的方法 - Call parent page's method from a user control 当我从页面访问用户控件时,无法从用户控件中的数据库中查看控件的值 - not able to see the controls values from DB in user control when i am access the user control from page 当 API 调用完成时,如何从 .RAZOR 主页面中的所有子组件触发/刷新我的 .RAZOR 主页面? - How can I trigger/refresh my main .RAZOR page from all of its sub-components within that main .RAZOR page when an API call is complete? Razor 页面 - 调用方法 - Razor Page - Call method C# ASP.NET 核心 Razor 页面 - 从 Razor 页面内部调用 PageModel 方法? - C# ASP.NET Core Razor page - call PageModel method from inside the Razor page? 我正在尝试制作一个提交类型按钮,单击该按钮时,会将用户重定向到另一个 Razor 类型页面 - I'm trying to make a submit type button that, when clicked on, redirects the user to another Razor type page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM