简体   繁体   English

Razorpages 发布新记录并返回具有更新 Id 的页面

[英]Razorpages Post new record and return to page with updated Id

Using Net6.0 Razorpages, I have a form that returns a Person by Id, and allows the user to update the record or add a new Person OnPost.使用 Net6.0 Razorpages,我有一个按 Id 返回一个人的表单,并允许用户更新记录或添加一个新的 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.发布新记录时,我希望它返回同一页面并显示更新后的 Id,但它仍然为 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.当我检查 Person OnPost 时,我可以看到它已更新并且页面刷新,只是没有填充新 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.检查你的PrimaryKey设计, PersonId是数据库中的主键,当用户发布新记录时, PersonId的值是自增的,而不是依赖于新记录中PersonId的值。 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 .当用户要发布一条新记录时, PersonId的输入框会自动锁定,不允许用户输入任何内容,所以页面不会接收到PersonId的值,页面刷新后,不会显示 PersonId 的真实值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.在我看来,向用户显示主键是没有必要的,但如果你想这样做,你可以通过PersonId查询并在添加新记录后将结果返回到页面,或者只是RedirectToPage("/Edit", new{ personId = Person.Id } ,我推荐你使用第二种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM