English 中文(简体)
在ASP.NET Core窗体中,以部分视图显示列表的最佳方式是什么?
原标题:What is the best way for Lists in partial view in an ASP.NET Core form?

我有一个类,我想用它作为复杂<;表单/>,下面是示例表单

// Model
public class Member
{
    [BsonId] 
    public ObjectId Id { get; set; }

    public string? Name { get; set; }
    public DateTime? Birthdate { get; set; }

    public List<MemberChild>? Children { get; set; } = new();
}

// MemberChild class
public class MemberChild
{
    public string? Name { get; set; }
    public DateTime? Birthdate { get; set; }
}

// Main form view for Member object
@model Member
<form method="post"
      autocomplete="off" action="/register">
        <div class="mb-2 ms-2">
            <label asp-for="Name"></label>
            <input asp-for="Name">
            <span asp-validation-for="Name></span>
        </div>
        <button>Add Child</button>

...and so on

现在,这个<code>MemberChild</code>模型有自己的局部视图,看起来是这样的:

// MemberChildForm.cshtml partial view
@model MemberChild

<div>
        <input placeholder="0"
               type="hidden"
               asp-for="Name"
               class="d-inline">
        <input placeholder="0"
               type="hidden"
               asp-for="Birthdate"
               class="d-inline">
</div>

它可以从该控制器操作中获取

[HttpGet("/children-form")]
public async Task<IActionResult> AssistanceDataForm()
{
    return PartialView("MemberChildForm", assistance);
}

我希望每当用户单击“+添加子项”按钮时,都会有一个新的MemberChildForm添加到表单中

我已经尝试过使用ajax来获得“/children-form”部分视图的html部分视图,但问题是<;输入/>名称的code>sasp与父<;表单/>,它应该是Member.Children[].Name,但它只显示Name,有内置的ASP Core解决方案吗?

为了让事情变得更容易,我想实现这样的东西https://cms.jotform.com/uploads/answers/answer/perkplanning/2110000_Screen%20Shot%202020-01-10%20at%209.30.38%20AM.png

问题回答

在您的局部视图中,您不需要使用asp来,我建议您直接使用名称属性,因为您可以自己在其中添加索引,这里有一个基于您的项目的简单演示,希望您能帮助您解决问题。

局部视图

@model int 
<div>
    <input placeholder="xxx"
           name="Children[@Model].name"
           class="d-inline">
    <input placeholder="0"
            type="date"
           name="Children[@Model].Birthdate"
           class="d-inline">
</div>

操作

[HttpGet("/children-form")]
public async Task<IActionResult> AssistanceDataForm(int index)
{
     return PartialView("MemberChild", index);
}

查看

@model Member
<form method="post"
      autocomplete="off" action="/register">
        <div class="mb-2 ms-2">
            <label asp-for="Name"></label>
            <input asp-for="Name">
            <span asp-validation-for="Name"></span>
        </div>
        <div class="mb-2 ms-2">
            <label asp-for="Birthdate"></label>
            <input asp-for="Birthdate">
            <span asp-validation-for="Birthdate"></span>
        </div>
        <input type="hidden" value="0" id="index"/>
        <div id="child">

        </div>
    <button type="button" id="add">Add Child</button>
        <button>Create</button>
</form>

<script>
    var index = document.getElementById("index").value

    document.getElementById("add").addEventListener( click ,e=>{
        $.ajax({
            type: "Get",
            url: "/children-form?index="+index,
            success: function(data){
                document.getElementById("child").insertAdjacentHTML("beforeend", data)
                index++; // Increment the index value
                document.getElementById("index").value = index; // Update the hidden field value
            }
        })
    })
</script>

Gif演示





相关问题
.NET 7 minimal API request timeout

I found this in .NET 8 (prerelease) but I can t use it due to company policy. https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0&viewFallbackFrom=aspnetcore-7.0 ...

Blazor web assembly pass checkbox list values to model

I m new to Blazor. I m working in a web assembly Blazor project. I am trying to create a form that passes values back to a model. I have it working with input text fields and drop downs, but I am ...

asp.net 6 c# mvc one to many get data from many side

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with ...

How to update shared view adminheader.cshtml from a model

I ve added a drop down into my shared adminheader.cshtl view and it needs to be dynamically updated from data from a table. So when I use another view like routeplan.cshtml I load the view from ...

Gitlab CI On Prem, Docker Image and ASP.NET Core 7

We have a .NET 6 application. We added CI using: image: mcr.microsoft.com/dotnet/sdk:6.0 before_script: - dotnet restore --packages $NUGET_PACKAGES_DIRECTORY build: stage: build script: - ...

ASP.NET Core 2 .1 - How to show all data in table

I don t know how to put CustomerData into CustomerLists property. I m already using CustomerLists = CustomerData; but I got error: CustomerLists is a type but is used like a variable Can anyone ...