如何在一次提交中输入多条记录的信息?

huangapple go评论73阅读模式
英文:

How do I input information for several records with one submission?

问题

我是ASP.NET Core MVC 6.0的初学者。按照Microsoft学习网站上的教程,我创建了"Contoso University"项目。在这个项目中,当我添加新的学生时,前往"创建新的"并输入一个人的信息,它是逐个进行的。

然而,我想要一次输入多个人的信息。例如,首先我填写了多条记录,然后点击提交按钮。

我该怎么做?

我找不到方法。

英文:

I am beginner to ASP.NET Core MVC 6.0. Following a tutorial from the Microsoft learning site, I created the "Contoso University" project. In this project, when I add the new student, head to "create new" and input information of on person. It walked one by one.

However, I want to input information of several persons at one time. For example, first I fill several records and click the submit button.

How can I do it?

I can not find my way.

答案1

得分: 2

"I want to input information of several persons at one time. For example, first I fill several records and click the submit button."

基于您的情景和要求,您应该在 asp form tag helper 内创建一个学生对象的列表,这需要一些JavaScript知识。

在表单内,您可以有一个按钮,该按钮会在表格中包含新的学生对象的DOM。最后,您可以将数据提交到您的控制器,控制器的参数应该定义为学生对象的列表。

让我们看看如何在实践中实现这个功能:

模型:

public class Student
{
    public int Id { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public int Age { get; set; }
}

视图:

<form method="post" asp-controller="Student" asp-action="Create">
    <div style="padding-bottom:20px">
        <button type="button" class="btn btn-primary" onclick="AddNewStudentFunc()">Add New Student</button>
    </div>

    <div id="dataTable">
        <table>
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody id="StudentList" data-count="0">
            </tbody>
        </table>
    </div>
    <input type="submit" class="btn btn-success" value="Submit" />
</form>

@section Scripts {
    <script>
        /*
             . I am Hidding table on load
        */
        document.getElementById('dataTable').style.display = 'none';

        function AddNewStudentFunc() {
            var countVal = parseInt($('#StudentList').attr('data-count'));
            var html = '';
            html += '<tr>';
            html += '<td><input type="text" name="Student[' + countVal + '].FirstName" class="form-control"/></td>';
            html += '<td><input type="text" name="Student[' + countVal + '].LastName" class="form-control"/></td>';
            html += '<td><input type="text" name="Student[' + countVal + '].Age" class="form-control"/></td>';
            html += '</tr>';

            $('#StudentList').append(html);
            countVal += 1;
            $('#StudentList').attr('data-count', countVal);
            /*
               . Showing table when adding item into it
            */
            document.getElementById('dataTable').style.display = 'block';
        }
    </script>
}

控制器:

[HttpPost]
public IActionResult Create(List<Student> student)
{
    if (ModelState.IsValid)
    {
        // 执行操作,比如将学生列表保存到数据库
    }
    if (!ModelState.IsValid)
    {
        return View("Create");
    }
    return View("Create");
}

输出:

如何在一次提交中输入多条记录的信息?

如何在一次提交中输入多条记录的信息?

如何在一次提交中输入多条记录的信息?

注意: 如果您需要更多详细信息,可以查看我们的官方文档

英文:

> I want to input information of several persons at one time. For
> example, first I fill several records and click the submit button.

Well, actually, based on your scenario and requirement you should create a list of student object inside the asp form tag helper which require a bit knowledge of javascript.

Within the form you would have a button which would include new dom for student object in the table. Finally, you can post to your controller where you would define list of student as parameter.

Let's have a look in practice, how we can achieve that:

Model:

<!-- language: c# -->
public class Student
{
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int Age { get; set; }
}

View:

<!-- language: c# -->
<form method="post" asp-controller="Student" asp-action="Create">
<div style="padding-bottom:20px">
<button type="button" class="btn btn-primary" onclick="AddNewStudentFunc()">Add New Student</button>
</div>

    &lt;div id=&quot;dataTable&quot;&gt;
        &lt;table&gt;
            &lt;thead&gt;
                &lt;tr&gt;
                    &lt;th&gt;First Name&lt;/th&gt;
                    &lt;th&gt;Last Name&lt;/th&gt;
                    &lt;th&gt;Age&lt;/th&gt;
                &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody id=&quot;StudenetList&quot; data-count=&quot;0&quot;&gt;
            &lt;/tbody&gt;
        &lt;/table&gt;
    &lt;/div&gt;
    &lt;input type=&quot;submit&quot; class=&quot;btn btn-success&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;

@section Scripts {
    &lt;script&gt;
        /*
             . I am Hidding table on load
        */
        document.getElementById(&#39;dataTable&#39;).style.display = &#39;none&#39;;

        function AddNewStudentFunc() {
            var countVal = parseInt($(&#39;#StudenetList&#39;).attr(&#39;data-count&#39;));
            var html = &#39;&#39;;
            html += &#39;&lt;tr&gt;&#39;;
            html += &#39;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;Student[&#39; + countVal + &#39;].FirstName&quot; class=&quot;form-control&quot;/&gt;&lt;/td&gt;&#39;;
            html += &#39;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;Student[&#39; + countVal + &#39;].LastName&quot; class=&quot;form-control&quot;/&gt;&lt;/td&gt;&#39;;
            html += &#39;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;Student[&#39; + countVal + &#39;].Age&quot; class=&quot;form-control&quot;/&gt;&lt;/td&gt;&#39;;
            html += &#39;&lt;/tr&gt;&#39;;

            $(&#39;#StudenetList&#39;).append(html);
            countVal += 1;
            $(&#39;#StudenetList&#39;).attr(&#39;data-count&#39;, countVal);
            /*
               . Showing table when adding item into it
            */
            document.getElementById(&#39;dataTable&#39;).style.display = &#39;block&#39;;

        }
    &lt;/script&gt;
}

Controller:
<!-- language: c# -->
[HttpPost]
public IActionResult Create(List<Student> student)
{
if (ModelState.IsValid)
{

            }
            if (!ModelState.IsValid)
            {
                return View(&quot;Create&quot;);
            }
            //Save your student list into database
            //_context.Studnet.AddRange(student);
            //_context.SaveChangesAsync();
            return View(&quot;Create&quot;);
        }

Output:

如何在一次提交中输入多条记录的信息?

如何在一次提交中输入多条记录的信息?

如何在一次提交中输入多条记录的信息?

Note: If you need more details, you could check our official document here.

huangapple
  • 本文由 发表于 2023年5月26日 00:15:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定