在ASP.NET MVC中插入多行 [英] Inserting Multiple rows in ASP.NET MVC
问题描述
我无法在数据库中插入多个数据.这是下面的代码:
I'm having trouble inserting multiple data on my database.. This is my code below:
上下文类:
public void Add(IEnumerable<UserInformation> model)
{
foreach(UserInformation user in model)
{
_db.Add(user);
}
_db.SaveChanges();
}
这是我的控制者:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Add(IEnumerable<UserInformation> userInformation)
{
if (!ModelState.IsValid)
{
}
_ui_context.Add(userInformation);
return new JsonResult("User Saved!");
}
这是我的观点:
@model IEnumerable<MyMVC.Models.UserInformation>
<form method="POST" id="form_data">
<input type="hidden" id="action_value" value="" />
<table id="tbl_data" class="table table-bordered">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Location</th>
</tr>
</thead>
<tbody id="table_data">
<tr>
<td>
<input type="hidden" id="txtId" class="form-control" />
<input name="firstname" class="form-control" id="txtFirst" />
<span class="text-danger"></span>
</td>
<td>
<input name="lastname" class="form-control" id="txtLast" />
<span class="text-danger"></span>
</td>
<td>
<input name="location" class="form-control" id="txtLocation" />
<span class="text-danger"></span>
</td>
</tr>
</tbody>
</table>
</form>
<a class="btn btn-info" id="btnAddField">Click to Add more Fields</a>
这是我的AJAX:
添加字段中的代码
//Add fields
var i = 1;
$(document).on("click", "#btnAddField", function () {
$("#table_data").append("<tr>"
+ "<td><input asp-for='FirstName' name='firstname' class='form-control' id='txtFirst" + i + "' />"
+ "<span class='text-danger'></span ></td>"
+ '<td><input asp-for="LastName" name="lastname" class="form-control" id="txtLast' + i + '" />'
+ '<span class="text-danger" ></span ></td>'
+ '<td><input asp-for="Location" name="location" class="form-control" id="txtLocation'+i+'" />'
+ '<span class="text-danger"></span ></td>'
+ "</tr > ");
i++;
});
POST中的代码
var data = $('#form_data').serialize();
$.ajax({
method: "POST",
url: "/UserInformation/Add",
data: data,
dataType: "JSON",
success: function (data) {
alert(data);
clearFields();
$("#exampleModal").modal("hide");
loadUser();
}
});
每次调试时,我都会在ajax上得到空响应.如果您能帮助我,我非常感谢.
I'm getting a null response on ajax every time I debugging it. I am very thankful if you would help me with this.
谢谢!
推荐答案
为使模型绑定正常工作,您需要在name属性中为字段提供索引.看看这个问题,我回答一个非常相似的问题:
For model binding to work you need to give you fields an index in the name property. Have a look at this question where I answered a very similar question:
因此,您的代码需要看起来类似于以下内容,但请注意,在javascript中使用 asp-for
可能不起作用,因为要使其正常工作,标记帮助程序必须呈现在服务器端,您将html附加到dom的位置.
So you code needs to look something like the below, but note that using asp-for
in javascript probably won't work because for that to work the tag helper has to be rendered server side, where you're just attaching html to the dom.
您还需要保持索引的连续总数,因为每个 i
对于该组UserInformation都必须是唯一的.
You also need to keep a running total for the index as each i
needs to be unique for that group of UserInformation.
<tr>
<td>
<input type="hidden" id="txtId" class="form-control" />
<input name="UserInformation[0].firstname" class="form-control" id="txtFirst" />
<span class="text-danger"></span>
</td>
<td>
<input name="UserInformation[0].lastname" class="form-control" id="txtLast" />
<span class="text-danger"></span>
</td>
<td>
<input name="UserInformation[0].location" class="form-control" id="txtLocation" />
<span class="text-danger"></span>
</td>
</tr>
//Add fields
var i = 1;
$(document).on("click", "#btnAddField", function () {
$("#table_data").append("<tr>"
+ "<td><input asp-for='FirstName' name='UserInformation[i].firstname' class='form-control' id='txtFirst" + i + "' />"
+ "<span class='text-danger'></span ></td>"
+ '<td><input asp-for="LastName" name="UserInformation[i].lastname" class="form-control" id="txtLast' + i + '" />'
+ '<span class="text-danger" ></span ></td>'
+ '<td><input asp-for="Location" name="UserInformation[i].location" class="form-control" id="txtLocation'+i+'" />'
+ '<span class="text-danger"></span ></td>'
+ "</tr > ");
i++;
});
这篇关于在ASP.NET MVC中插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!