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

sunny
  • 2,670
  • 5
  • 24
  • 39
  • You might look into `Session`. Here you can save data only for that user. – Poul Bak May 25 '22 at 23:13
  • @PoulBak, Actually, I think TempData would be more appropriate for this task than Session in Razor Pages; however, the best answer is sunny's own answer, which he should accept as the Accepted answer. – Lee Cichanowicz Aug 12 '23 at 18:34

2 Answers2

2

The simple answer was :

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

My mistake was

... new{asklib = AskLibrarian});

After more than two hours

sunny
  • 2,670
  • 5
  • 24
  • 39
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.

Jeff Putz
  • 14,504
  • 11
  • 41
  • 52
  • 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 – sunny May 25 '22 at 21:47
  • 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. – Jeff Putz May 26 '22 at 01:15
  • @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. – Lee Cichanowicz Aug 12 '23 at 18:37