[英]Can collections be bound to the page model in Razor Pages?
I have two collections that more or less look like this: 我有两个或多或少看起来像这样的集合:
[BindProperty]
public List<MyObject> MyObjects { get; set; } = new List<MyObject>();
[BindProperty]
public List<MyOtherObject> MyOtherObjects { get; set; } = new List<MyOtherObject>();
So they're bounding to the server, but I'm not quite sure how to bind them to the client. 因此,它们将绑定到服务器,但是我不太确定如何将它们绑定到客户端。 Or I'm just doing it wrong. 或者我只是做错了。 The lists are adding to an HTML table when the page loads, but there's no direct binding: 页面加载时,列表将添加到HTML表中,但是没有直接绑定:
@foreach (var item in Model.MyObjects)
{
<tr>
<td>
Title
</td>
<td>
@Html.DisplayFor(m => item.Property)
</td>
</tr>
}
The issue is that I need to POST to the server to add items to each list at different points. 问题是我需要发布到服务器以在每个点的各个列表中添加项目。 And when I do that, I just return Page();
当我这样做时,我只return Page();
. 。 I'm not redirecting because at this point, nothing is being saved to the database and the form isn't complete. 我没有重定向,因为此时,什么都没有保存到数据库,并且表单还不完整。
From my current knowledge, I figure I have two options: redirect and pass the data in the query string or save the data to a cookie and repopulate on every POST. 根据我目前的知识,我认为我有两个选择:重定向并在查询字符串中传递数据,或将数据保存到cookie并在每次POST时重新填充。
Both aren't great options. 两者都不是很好的选择。 Is there a better way to do this? 有一个更好的方法吗?
Model Binding is designed to work with form values. 模型绑定旨在与表单值一起使用。 If you aren't repopulating your collections from persisted data on the server, you need to add form fields for each item to get Model Binding to work. 如果您不是从服务器上的持久数据中重新填充集合,则需要为每个项目添加表单字段以使“模型绑定”起作用。 If you are creating a multi-step, wizard-like form, you usually persist unsaved values from each step in hidden fields from one step to the next. 如果要创建多步,类似向导的表单,则通常会将每个步骤的未保存值保留在一个步骤到下一个步骤的隐藏字段中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.