[英]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.