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

Time:01-03

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

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

@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 >
    <h1>Add Address</h1>
</div>
<form method="post">
    <div >
        <div >
            <div >
                <select name = "country" asp-items="@Model.Country"> 
    </select> 
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.State" />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.City" 
    />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.StreetNumber" 
    placeholder="Street #" />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.StreetName" 
     placeholder="Street Name" />
            </div>
        </div>
        <div >
            <div >
                <div >
            <div >
                <input type="text" asp-for="Address.AppNumber" 
  placeholder="App#" />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.ZipCode" />
            </div>
        </div>
        <div >
            <div >
                <input type="tel" asp-for="Address.Phone"  />
            </div>
        </div>
        <div >
            <div >
                <input type="tel" asp-for="Address.CellPhone" />
            </div>
        </div>
        <div >
            <div >
                <button type="submit">Submit</button>
            </div>
        </div>
    </div>
</form>

CodePudding user response:

Firstly,you need to pass User1.FirstName when form post,so that you can get User1.FirstNamein OnPost handler.

form(add hidden input with User1.FirstName):

<form method="post">
    <div >
        <div >
            <div >
                <select name = "country" asp-items="@Model.Country"> 
    </select> 
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.State" />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.City" 
    />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.StreetNumber" 
    placeholder="Street #" />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.StreetName" 
     placeholder="Street Name" />
            </div>
        </div>
        <div >
            <div >
                <div >
            <div >
                <input type="text" asp-for="Address.AppNumber" 
  placeholder="App#" />
            </div>
        </div>
        <div >
            <div >
                <input type="text" asp-for="Address.ZipCode" />
            </div>
        </div>
        <div >
            <div >
                <input type="tel" asp-for="Address.Phone"  />
            </div>
        </div>
        <div >
            <div >
                <input type="tel" asp-for="Address.CellPhone" />
            </div>
        </div>
        <div >
            <div >
                <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):

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

CodePudding user response:

You have to show your cshtml file i.e. the front end of the Razor page for a more clear description of your problem. 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. It should be something like this <select name="Address.Country" asp-items="Model.Country"></select> Notice the name of the select element Address.Country i.e. it reflects the full path to the target property. 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.

  • Related