通过剃须页之间的模型数据

发布于 2025-02-01 08:18:19 字数 2255 浏览 1 评论 0原文

我尝试在两个剃须刀页面之间通过模型数据,数据不是字符串或bool或int 我想要传递到第二页的数据是一个模型, 我以这种方式进行taht,

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; }
}

然后在上获取方法以这种方式传递数据:

 [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 });
    }

在第二页中 post 方法,喜欢以这种方式获取数据:

public void OnGet(Model.AskLibrarian asklib)
    {
        askLibrarianVM = new AskLibrarianVM
        {
            Answered = false,
            CreateDate = asklib.CreateDate,
            LastUpdate = asklib.CreateDate,
            RandomeCode = asklib.reciptCode,
            Status = false,

        };
    }

但是 asklib 是空的,我在的末端设置了一个断点,然后播下 asklib 如果填充有效的值,但是在 post方法中,当我尝试获取数据时,Asklib是 empty 我的错误是什么

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 = 
quot;{ 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城空念 2025-02-08 08:18:19

简单的答案是:

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

我的错误是

... new{asklib = AskLibrarian});

超过两个小时后

The simple answer was :

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

My mistake was

... new{asklib = AskLibrarian});

After more than two hours

迟月 2025-02-08 08:18:19

最低的摩擦方式是返回视图(“ someviewforthemodel”,AskLibrarian),并以完全不同的视图来完成您的操作。您的第二页控制器操作确实没有做任何事情。

否则,您必须保存与AskLibrarian对象关联的ID,大概是在数据库中,然后通过将ID放在URL路径中来查找第二页(请务必确保验证用户应该看到它!),或通过在数据库中查找用户拥有的任何内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文