英文:
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>
<div id="dataTable">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</thead>
<tbody id="StudenetList" 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($('#StudenetList').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>';
$('#StudenetList').append(html);
countVal += 1;
$('#StudenetList').attr('data-count', countVal);
/*
. Showing table when adding item into it
*/
document.getElementById('dataTable').style.display = 'block';
}
</script>
}
Controller:
<!-- language: c# -->
[HttpPost]
public IActionResult Create(List<Student> student)
{
if (ModelState.IsValid)
{
}
if (!ModelState.IsValid)
{
return View("Create");
}
//Save your student list into database
//_context.Studnet.AddRange(student);
//_context.SaveChangesAsync();
return View("Create");
}
Output:
Note: If you need more details, you could check our official document here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论