I try pass model data between two Razor pages, data is not string or bool or int data that i want pass to second page is a model, i do taht with this way,
public class AskLibrarian
{
public int Id { get; set; }
public string FullName { get; set; }
public string Subject { get; set; }
public string Email { get; set; }
public string Text { get; set; }
public string UserIp { get; set; }
public DateTime CreateDate { get; set; }
public bool ReadIt { get; set; }
public bool Answer { get; set; }
public string reciptCode { get; set; }
}
And on Get method pass data with this way:
[BindProperty]
public AskLibrarian AskLibrarian { get; set; }
public async Task<IActionResult> OnPostQuestionAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
AskLibrarian.Answer = false;
AskLibrarian.CreateDate = DateTime.Now;
AskLibrarian.ReadIt = false;
string userIp = $"{ HttpContext.Connection.RemoteIpAddress}";
if (string.IsNullOrEmpty(userIp))
{
userIp = "127.0.0.1";
}
AskLibrarian.UserIp = userIp;
string rndNuber = Business.RandomNumberForQuestion.randCode;
AskLibrarian.reciptCode = rndNuber;
await _emailSenderService.SendEmailAsync(AskLibrarian.Email, AskLibrarian.FullName, rndNuber);
_context.AskLibrarians.Add(AskLibrarian);
await _context.SaveChangesAsync();
Message = "your message sended";
//return RedirectToPage("/Subfolder/Index", new { SFId = 7 });
return RedirectToPage("/Subfolder/AskLibrarianCode", new { asklib = AskLibrarian });
}
In post method in second page, like to get data on this way:
public void OnGet(Model.AskLibrarian asklib)
{
askLibrarianVM = new AskLibrarianVM
{
Answered = false,
CreateDate = asklib.CreateDate,
LastUpdate = asklib.CreateDate,
RandomeCode = asklib.reciptCode,
Status = false,
};
}
But asklib is empty ,I set a breakpoint at end of Get method and I sow that asklib if filled with valid values but in post method when i try to get data, asklib is empty what is my mistake
CodePudding user response:
The lowest friction way is to return View("SomeViewForTheModel", AskLibrarian)
and do your thing with a completely different view. Your second page controller action really isn't doing anything.
Otherwise, you'll have to save the ID associated with your AskLibrarian
object, presumably in a database, and then look it up on the second page either by putting the ID in the URL path (be sure to validate the user should see it!), or by looking up in the database whatever is owned by the user.
CodePudding user response:
The simple answer was :
return RedirectToPage("/Subfolder/AskLibrarianCode", AskLibrarian );
My mistake was
... new{asklib = AskLibrarian});
After more than two hours