RazorPages发布新记录,然后返回带有更新ID的页面
使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查您的型号设计,
themid
是数据库中的primarykey
,当用户发布新记录时,personid
的值是自我提示,而不是根据新记录中的themid
的值。当用户想发布新记录时,将自动锁定themid
的输入框,不允许用户输入任何内容,因此该页面将无法接收personid
的值,在刷新页面后,它将不会显示themid
的实际值。我认为,向用户显示主要键是不需要的,但是如果您想这样做,则可以通过
personid
查询并在添加新记录后将结果返回到页面上。代码> redirecttopage(“/edit”,new {personid = person.id} ,我建议您使用第二种方法。Check your model design,
PersonId
is thePrimaryKey
in database, when user post a new record, The value ofPersonId
is self-incrementing rather than depending on the value of thePersonId
in the new record. When user want to post a new record, The input box ofPersonId
will be automatically locked, Not allowing user to enter anything, So the page will not receive the value ofPersonId
, after the page refresh, It will not display the real value ofPersonId
.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 justRedirectToPage("/Edit", new{ personId = Person.Id }
, I recommend you to use the second method.