2

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

1
  • You might look into Session. Here you can save data only for that user. Commented May 25, 2022 at 23:13

2 Answers 2

2

The simple answer was :

 return RedirectToPage("/Subfolder/AskLibrarianCode", AskLibrarian ); 

My mistake was

... new{asklib = AskLibrarian}); 

After more than two hours

Sign up to request clarification or add additional context in comments.

1 Comment

What does your mistake line mean? Where was that line? Did you remove it to resolve the issue? Or did you replace it with the return RedirectToPage line?
0

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.

3 Comments

i want send all data from first page to second page, another hand its not good way connect to the databse without any result when I can pass data directly, it's important for performance
Well, these are really your only options. Unless you're going to be handling extraordinary volume, worrying about hitting the database is a little premature.
@JeffPutz, I think you are referring to MVC, not Razor Pages, with speaking of Views, instead of Pages or Partial Pages, though here, the concept is much the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.