I have one page that collects the users initials and the entry date. I then pass that to as secondary page where some input work is going to be done but I have not been able to get the initials or entry date to show up in the HTML on the secondary page.
I am passing Initials and the EntryDate from the index page to the Add page with this.
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
return RedirectToPage("Add", "User",
new { User.EntryDate, User.Initials});
}
I can confirm that the object (Initials, EntryDate) makes it to the second page by setting a breakpoint here on the Add page.
public void OnGet(UserModel user)
{
Initials = user.Initials;
EntryDate = user.EntryDate;
}
Lastly, I can transfer the Initials and the EntryDate from User object to the Item object so that the Initials and EntryDate are included with the OnPost when the data is submitted to the database.
[ViewData]
[BindProperty]
public DateTime EntryDate { get; set; }
[ViewData]
[BindProperty]
public string Initials { get; set; }
public async Task<IActionResult> OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
Initials = User.Initials;
EntryDate = User.EntryDate;
ItemModel item = new ItemModel();
item.UserId = Initials;
item.TransferDate = EntryDate;
item.ItemNumber = Item.ItemNumber;
item.FromLocation = Item.FromLocation;
item.ToLocation = Item.ToLocation;
item.Quantity = Item.Quantity;
await item.InsertAsync();
return RedirectToPage("/Index");
}
The part that I am struggling with is that I want to show the Initials and EntryDate at the top of the Add page when it is loaded.
<div class="row">
<div class="col-md-4">
Initials: @Html.DisplayFor(User => User.User.Initials)
<br />
Entry Date: @Html.DisplayFor(User => User.User.EntryDate)
</div>
</div>
I did notice the Initials and EntryDate show up on the @Html.DisplayFor when I hit the OnPost. My issue is that I want them to show up on the OnGet (on the Add page before the form on that page has any data entered into it.)
I realize that the way I posted the code it may be confusing, so I am going to post the entire page as it is now.
public class AddModel : PageModel
{
[ViewData]
[BindProperty]
public DateTime EntryDate { get; set; }
[ViewData]
[BindProperty]
public string Initials { get; set; }
[BindProperty]
public ItemModel Item { get; set; }
[BindProperty]
public UserModel User { get; set; }
public void OnGet(UserModel user)
{
Initials = user.Initials;
EntryDate = user.EntryDate;
}
public async Task<IActionResult> OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
Initials = User.Initials;
EntryDate = User.EntryDate;
ItemModel item = new ItemModel();
item.UserId = Initials;
item.TransferDate = EntryDate;
item.ItemNumber = Item.ItemNumber;
item.FromLocation = Item.FromLocation;
item.ToLocation = Item.ToLocation;
item.Quantity = Item.Quantity;
await item.InsertAsync();
return Page();
//return RedirectToPage("/Add");
//return RedirectToPage("/Index");
}
}
From your description, You seems to wanna direct to Add
page with some values, then load Add page with these values finally submit the from with these values, I create a demo to reproduce your issue, Hope it can give you some help.
[BindProperty]
public UserModel User { get; set; }
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
return RedirectToPage("Add",
new { User.EntryDate, User.Initials });
}
Add.cshtml.cs
public class AddModel : PageModel
{
[BindProperty]
[FromForm]
public ItemModel Item { get; set; } = new ItemModel();
public void OnGet(string Initials, DateTime EntryDate)
{
Item.Initials = Initials;
Item.EntryDate = EntryDate;
}
public void OnPost()
{
// do some actions......
}
}
Add.cshtml
@page
@model RazorP.Pages.AddModel
@{
}
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row">
<div class="col-md-4">
Initials: @Html.DisplayFor(x=>x.Item.Initials)
<input type="hidden" asp-for="Item.Initials" />
<br />
Entry Date: @Html.DisplayFor(x=>x.Item.EntryDate)
<input type="hidden" asp-for="Item.EntryDate" />
</div>
</div>
<div class="form-group-with-label login-input-password mb-2">
<label>Age</label>
<input class="form-control-input" asp-for="Item.Age" autocomplete="off" />
<span class="fa fa-eye"></span>
<span asp-validation-for="Item.Age" class="text-danger"></span>
</div>
<div class="form-group-with-label login-input-password mb-2">
<label>Address</label>
<input class="form-control-input" asp-for="Item.Address" autocomplete="off" />
<span class="fa fa-eye" ></span>
<span asp-validation-for="Item.Address" class="text-danger"></span>
</div>
<button>Submit</button>
</form>