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