简体   繁体   English

ASP.Net Core Razor Pages动态列表

[英]ASP.Net Core Razor Pages dynamic list

I want to create a website with Razor pages to maintain spaces and the according rooms. 我想用Razor页面创建一个网站来维护空间和相应的房间。 So, in my model a space has a list of rooms. 所以,在我的模型中,空间有一个房间列表。

public class Space
{
    public int Id { get; set; }
    public string SpaceName { get; set; }
    public List<Room> Rooms { get; set; }

    public Space()
    {
        this.Rooms = new List<Room>();
    }
}

In the create.cshtml I want to dynamically add and remove rooms 在create.cshtml中,我想动态添加和删除房间

<h1>Create New Space</h1>
<form method="post">
    <div class="form-group">
       <label for="Space.SpaceName">Space Name</label>
       <input type="text" class="form-control" asp-for="Space.SpaceName" placeholder="Enter space name">
       <span asp-validation-for="Space.SpaceName" ></span>
    </div>
    @if(Model.Space != null){
        for (int i = 0; i < Model.Space.Rooms.Count; i++)
        {
         <input asp-for="@i" type="hidden" />
         <div class="form-group">
             <label for="SpaceName">Room Name</label>
             <input type="text" class="form-control" asp-for="Space.Rooms[i].Name" placeholder="Enter space name">
             <span asp-validation-for="Space.Rooms[i].Name" ></span>
         </div>

         <button type="submit" asp-page-handler="removeroom" asp-route-index="@i">Remove Room</button>
         }
    }

    <button type="submit" asp-page-handler="addroom">Add Room</button>

    <button type="submit" asp-page-handler="submit" class="btn btn-primary">Submit</button>
</form>

The PageModel looks like this: PageModel看起来像这样:

public class CreateModel : PageModel
{
    readonly SpaceRepository spaceRepository;

    public CreateModel(SpaceRepository spaceRepository)
    {
        this.spaceRepository = spaceRepository;
    }

    [BindProperty]
    public Space Space { get; set; }

    public async Task<IActionResult> OnPostSubmitAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        spaceRepository.Create(Space);
        return RedirectToPage("./Index");
    }

    public async Task OnPostAddRoomAsync()
    {
        Space.Rooms.Add(new Room());
    }

    public async Task OnPostRemoveRoomAsync(int index)
    {
        Space.Rooms.RemoveAt(index);
    }
}

So far it works as expected, but when I create for example 3 rooms and delete the first or second one, it always deletes the last one. 到目前为止,它按预期工作,但是当我创建例如3个房间并删除第一个或第二个房间时,它总是删除最后一个房间。

The page seems not to receive the manipulated list after the handler post. 在处理程序发布后,该页面似乎没有收到操作列表。

Any ideas what's missing? 有什么想法缺少什么?

Thank you for your help! 谢谢您的帮助!

I know you probably don't need it anymore, but I also had a problem with that so I opened an issue on github and I got an answer for your question: https://github.com/aspnet/Mvc/issues/8373 我知道你可能不再需要了,但我也遇到了问题所以我在github上打开了一个问题,我得到了你的问题的答案: https//github.com/aspnet/Mvc/issues/8373
So basically MVC is round tripping model, so the fields can remain filled. 所以基本上MVC是圆形跳闸模型,因此字段可以保持填充。 Because of that, page model uses last known modelstate to fill every field. 因此,页面模型使用最后已知的模型状态来填充每个字段。 You can fix that by using Modelstate.Clear() after you delete your item. 您可以在删除项目后使用Modelstate.Clear()来解决此问题。 Thanks to that you will also not show any validation message after just deleting item from list. 因此,在从列表中删除项目后,您也不会显示任何验证消息。

The indexes MUST be in sequential order without gaps. 索引必须按顺序排列,没有间隙。 If you have a array or list like this: 如果您有这样的数组或列表:

rooms[0] = "Room 1"
rooms[1] = "Room 2"
rooms[2] = "Room 3"

It will post correctly. 它会正确发布。 But then if you remove rooms[1] , it will only post rooms[0] and skip rooms[2] . 但是如果你删除rooms[1] ,它只会发布rooms[0]和跳过rooms[2]

This is solved using GUID's as indexes, which is how the BeginCollectionItem or the EditorForMany with hidden index GUID's keep track of this in proper sequential order. 这是使用GUID作为索引来解决的,这是BeginCollectionItem或带有隐藏索引GUID的EditorForMany以正确的顺序跟踪它的方式。

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

相关问题 ASP.NET Core Razor页面的路由 - Routing for ASP.NET Core Razor Pages Razor Pages 中的 Asp.net 核心本地化 - Asp.net core Localization in Razor Pages 重命名 ASP.NET 核心 Razor 页面中的页面/共享目录 - Rename Pages/Shared directory in ASP.NET Core Razor Pages 无法在选择列表中保存多个选择 - asp.net core web app razor pages - Can't save multiple selections in select list - asp.net core web app razor pages 如何从 Asp.net 核心 Razor 页面中的 IdentityUser 列表中获取没有角色的用户 - How to Get the Users with No Role from IdentityUser List in Asp.net Core Razor Pages ASP.NET Core Razor 页面 - 获取要在 Chart.js 图形上显示的列表数据 - ASP.NET Core Razor Pages - Get List Data to Display on Chart.js Graph 在 ASP.NET 核心 Razor 页面中填充下拉列表时获取 null 引用异常 - Getting null reference exception when populating dropdown list in ASP.NET Core Razor Pages 带有List的asp.net网页/ Razor WebGrid <dynamic> 数据源,排序问题 - asp.net Web Pages / Razor WebGrid with List<dynamic> datasource, sorting issue 如何从 razor 查看带有动态列表的 model 中的列表? ASP.NET 内核 3.1 - How to submit from a razor view a list in a model with dynamic list? ASP.NET Core 3.1 asp.net core razor pages 支持delete和put请求 - asp.net core razor pages support for delete and put requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM