简体   繁体   English

集合可以绑定到Razor Pages中的页面模型吗?

[英]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.

相关问题 发布时如何清除 Razor 页面模型上的绑定属性? - How can you clear a bound property on a Razor Page's model when POSTing? Razor Pages OnPost - 父页面是否可以使用在创建时传递给它的部分的相同部分模型? - Razor Pages OnPost - Can the parent page use the same partial model that is passed to its partial on creation? Razor 页面:从页面获取字符串列表到页面模型 - Razor Pages: Get List of Strings from Page into Page Model 在 razor 页面项目中找不到页面 -.razor,净值 5.0 - Page can’t be found - .razor in razor pages project, Net 5.0 使用Razor页面向控制器或页面模型发出AJAX请求 - Making an AJAX request to a controller or page model with Razor Pages 无法访问 Razor 页面中的 Model 项目 - Can't access Model items in Razor Page 如何在 dotnet core razor 页面中打开新创建的 Model Razor 页面 (3.1) - How to open a new created Model Razor Page in dotnet core razor pages (3.1) 重定向到 Razor 页面中的登录页面后无法读取 ReturnUrl - Can't read ReturnUrl after redirecting to login page in Razor Pages Model属性在剃刀页面中完成了绑定的输入值的大部分工作 - Model property does much the value of the input it's bound within razor page 如何更改绑定到 Razor 页面上模型的特定文本的字体颜色? - How do I change the font color of a specific text bound to a model on a Razor page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM