RazorPages发布新记录,然后返回带有更新ID的页面

发布于 2025-01-28 17:41:08 字数 1488 浏览 1 评论 0原文

使用ASP.NET Core Razor页面,我有一个表格,可以通过ID返回一个人,并允许用户更新记录或添加新的人Onpost。

发布新记录时,我希望它返回到同一页面并显示更新的ID,但仍为0。


    public class Person
    {
        public int PersonId { get; set; }
        public string Name { get; set; }
        public string FavouriteDoughnut { get; set; }
    }

表格:

   @page "{personId:int}"
   @model Pages.EditModel

   <form method="post">
      <label asp-for="Person.PersonId">Id</label>
      <input asp-for="Person.PersonId" readonly>

      <label asp-for="Person.Name">Name</label>
      <input asp-for="Person.Name">

      <label asp-for="Person.FavouriteDoughnut">Doughnut?</label>
      <input asp-for="Person.FavouriteDoughnut">

      <button type="submit>Save</button>
   </form>
  
    [BindProperty(SupportsGet=true)]
    public Person Person { get; set; }

    public async void OnGet(int personId)
    {
        if(personId > 0)
        {
            Person = await _dbContext.People.FindAsync(personId);

            return;
        }
            Person = new Person();
    }

    public void OnPost()
    {
        Person = UpdatePerson(Person);
    }

    private void UpdatePerson(Person person)
    {
        _dbContext.People.Update(person);
        await _dbContext.SaveChanges();
        return person;
    }

我需要重定向到编辑页面吗?当我检查人员Onpost时,我可以看到它已更新并且页面刷新,它只是没有新ID的填充。

Using ASP.NET Core Razor Pages, I have a form that returns a Person by Id, and allows the user to update the record or add a new Person OnPost.

When posting a new record, I expect it to return to the same page and display the updated Id, but it remains as 0.


    public class Person
    {
        public int PersonId { get; set; }
        public string Name { get; set; }
        public string FavouriteDoughnut { get; set; }
    }

The form:

   @page "{personId:int}"
   @model Pages.EditModel

   <form method="post">
      <label asp-for="Person.PersonId">Id</label>
      <input asp-for="Person.PersonId" readonly>

      <label asp-for="Person.Name">Name</label>
      <input asp-for="Person.Name">

      <label asp-for="Person.FavouriteDoughnut">Doughnut?</label>
      <input asp-for="Person.FavouriteDoughnut">

      <button type="submit>Save</button>
   </form>
  
    [BindProperty(SupportsGet=true)]
    public Person Person { get; set; }

    public async void OnGet(int personId)
    {
        if(personId > 0)
        {
            Person = await _dbContext.People.FindAsync(personId);

            return;
        }
            Person = new Person();
    }

    public void OnPost()
    {
        Person = UpdatePerson(Person);
    }

    private void UpdatePerson(Person person)
    {
        _dbContext.People.Update(person);
        await _dbContext.SaveChanges();
        return person;
    }

Do I need to redirect to the Edit page? When I inspect the Person OnPost I can see that it is updated and the page refreshes, it just doesn't get populated with the new Id.

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

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

发布评论

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

评论(1

小草泠泠 2025-02-04 17:41:08

检查您的型号设计,themid是数据库中的primarykey,当用户发布新记录时,personid的值是自我提示,而不是根据新记录中的themid的值。当用户想发布新记录时,将自动锁定themid的输入框,不允许用户输入任何内容,因此该页面将无法接收personid的值,在刷新页面后,它将不会显示themid的实际值。

我认为,向用户显示主要键是不需要的,但是如果您想这样做,则可以通过personid查询并在添加新记录后将结果返回到页面上。代码> redirecttopage(“/edit”,new {personid = person.id} ,我建议您使用第二种方法。

Check your model design, PersonId is the PrimaryKey in database, when user post a new record, The value of PersonId is self-incrementing rather than depending on the value of the PersonId in the new record. When user want to post a new record, The input box of PersonId will be automatically locked, Not allowing user to enter anything, So the page will not receive the value of PersonId, after the page refresh, It will not display the real value of PersonId.

In my opinion, Showing the primary key to the user is not necessary, But if you want to do that, You can query by PersonId and return the result to the page after adding the new record or just RedirectToPage("/Edit", new{ personId = Person.Id }, I recommend you to use the second method.

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