0

对于我的项目,我需要创建学校并添加在那里学习的学生。但是学校和学生需要在同一个视图中创建。

[1]:https://i.stack.imgur.com/OPjTL.png

示例可以在照片上看到。应输入学校名称。在添加学生按钮上,将显示输入学生姓名和年龄的模式弹出窗口。单击弹出窗口中的添加后,应将用户添加到列表中。您可以添加任意数量的学生。最后,点击保存时,添加了所有学生的学校应发送到控制器,以便将它们保存在数据库中。

视图模型示例

public class SchoolViewModel
{
    public string Name { get; set; }
    public List<Students> Students { get; set; }
}

public class Students
{
    public string StudentsName { get; set; }
    public int Age { get; set; }
}

我想不出一种将学生动态添加到列表中的方法,以及如何绑定和临时存储到 schoolviewmodel 以便将他们的数据放在最后一个保存按钮发布方法上

4

1 回答 1

0

我创建了一个演示,将弹出模态放置在部分视图中以输入学生的姓名和年龄。输入姓名和年龄后,单击Add模态按钮,然后它将输入数据附加到主视图的学生表中。

最后,点击 SAVE 按钮时使用 ajax 发送所有数据。以下是我演示的步骤:

1.创建局部视图/Views/Shared/_AddStudentPartial.cshtml

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Add Student</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <div class="col-4">
                <label> Student Name</label>
                <input id="tempName" name="tempName" class="form-control" />
            </div>

            <div class="col-4">
                <label> Student Age</label>
                <input id="tempAge" name="tempAge" class="form-control" />
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" id="addStudent" class="btn btn-primary">Add</button>
        </div>
    </div>

</div>

2.AddSchool.cshtml

@model SchoolViewModel
<h1>AddSchool</h1>
<div class="row">
    <div class="col-md-4">
        <form>
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label class="control-label">School Name</label>
                <input asp-for="Name" class="form-control" />
            </div>
            <table class="table">
                <thead>
                    <tr>
                        <th>
                            Student Name
                        </th>
                        <th>
                            Student Age
                        </th>
                    </tr>
                </thead>
                <tbody id="studentList"></tbody>
            </table>
            <div class="form-group">
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Add Student</button>
            </div>
            <div class="form-group">
                <input type="button" id="saveSchool" value="Save" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<partial name="_AddStudentPartial" />

@section Scripts{
    <script>
        $(function () {
            //append students to table
            $("#studentList").empty();
            $('#addStudent').on('click', function () {
                var name = $('#tempName').val();
                var age = $('#tempAge').val();

                $('<tr><td>' + name + '</td>' +
                    '<td>' + age + '</td>' +
                    '</tr>').appendTo($('#studentList'));

                $('#exampleModal').modal('hide');
                document.getElementById("tempName").value = "";
                document.getElementById("tempAge").value = "";
            });

            //save school
            $('#saveSchool').on('click', function () {

                var formData = new FormData();
                var schoolName = $('#Name').val();
                formData.append("name", schoolName);

                $("#studentList tr").each(function (i) {
                    $("td", this).each(function (j) {
                        if (j == 0) {
                            formData.append("Students[" + i + "].StudentsName", $(this).text());
                        } else {
                            formData.append("Students[" + i + "].Age", $(this).text());
                        }
                    });
                });


                $.ajax({
                    type: "POST",
                    url: "/Home/AddSchool",//use your url
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (response) {
                      //deal with the response
                   }

                });
            });
        });
    </script>
}

3.控制器

public IActionResult AddSchool()
{
    return View();
}
[HttpPost]
public IActionResult AddSchool(SchoolViewModel schoolViewModel)
{
    //save to database
}
于 2020-01-27T08:55:09.627 回答