Javascript 在数据表内使用表单时,Ajax调用是否返回空模型?
我在数据表中有一个引导表单,我正在更新该表单中的一个输入字段,实际上有多个输入字段,当我按下submit按钮时,如果在字段中输入任何新数据,它将在数据库中更新,我使用ajax调用发送数据,但我们知道数据表中有多个页面,所以首先页面数据已成功提交,但在其他页面上,ajax调用将空模型发送到控制器,从而导致空数据异常Javascript 在数据表内使用表单时,Ajax调用是否返回空模型?,javascript,ajax,asp.net-mvc,datatables,Javascript,Ajax,Asp.net Mvc,Datatables,我在数据表中有一个引导表单,我正在更新该表单中的一个输入字段,实际上有多个输入字段,当我按下submit按钮时,如果在字段中输入任何新数据,它将在数据库中更新,我使用ajax调用发送数据,但我们知道数据表中有多个页面,所以首先页面数据已成功提交,但在其他页面上,ajax调用将空模型发送到控制器,从而导致空数据异常 <form id="share"> @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @cl
<form id="share">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container col-md-12">
<table id="myTable" class="table table-hover table-striped table-bordered dataTable">
<thead>
<tr>
<th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().Id)</th>
<th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().TagName)</th>
<th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().TagCategory)</th>
<th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().TagValue)</th>
<th style="text-align:center"> Action</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.tags.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(m => Model.tags[i].Id)
@Html.HiddenFor(m => Model.tags[i].Id)
</td>
<td>
@Html.DisplayFor(m => Model.tags[i].TagName)
</td>
<td>
@Html.DisplayFor(m => Model.tags[i].TagCategory)
</td>
<td>
@Html.EditorFor(m => Model.tags[i].TagValue, new { htmlAttributes = new { @id = "TagVaule_" + Model.tags[i].Id, @class = "form-control" } })
@Html.ValidationMessageFor(m => Model.tags[i].TagValue, "", new { @class = "text-danger" })
</td>
<td>
@if (User.IsInRole("Admin"))
{
<button type="button" class="btn btn-danger" onclick="UpdateRow(@Model.tags[i].Id)">Update</button>
}
else
{
<button type="button" class="btn btn-danger" onclick="UpdateRow(@Model.tags[i].Id)" disabled>Update</button>
}
</td>
</tr>
}
</tbody>
</table>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content" id="myModalContent">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5 col-md-10">
@if (User.IsInRole("Admin"))
{
<button type="button" class="btn btn-danger" id="bulkupdate">BulkUpdate</button>
}
else
{
<button type="button" class="btn btn-danger" id="bulkupdate" disabled>BulkUpdate</button>
}
</div>
</div>
</div>
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@DisplayNameFor(m=>Model.tags.First().Id)
@Html.DisplayNameFor(m=>Model.tags.First().TagName)
@DisplayNameFor(m=>Model.tags.First().TagCategory)
@DisplayNameFor(m=>Model.tags.First().TagValue)
行动
@对于(int i=0;iModel.tags[i].Id)
@Html.HiddenFor(m=>Model.tags[i].Id)
@Html.DisplayFor(m=>Model.tags[i].TagName)
@DisplayFor(m=>Model.tags[i].TagCategory)
@Html.EditorFor(m=>Model.tags[i].TagValue,新的{htmlAttributes=new{@id=“TagVaule”+Model.tags[i].id,@class=“form control”})
@Html.ValidationMessageFor(m=>Model.tags[i].TagValue,“,新的{@class=“text danger”})
@if(User.IsInRole(“Admin”))
{
更新
}
其他的
{
更新
}
}
@if(User.IsInRole(“Admin”))
{
批量更新
}
其他的
{
批量更新
}
@节脚本{
$(文档).ready(函数(){
$('#myTable')。数据表({
“列”:[
无效的
无效的
无效的
{“宽度”:“20%”,
无效的
],
});
});
风险值形式=$(“#份额”);
$(文档).on('单击','批量更新')函数(){
调试器;
$.ajax({
键入:“GET”,
url:'@url.Action(“BulkUpdate”,“Home”),
数据:form.serialize(),
成功:功能(数据){
$('#myModalContent').html(数据);
$('myModal').modal('show');
}
});
});
//批量更新确认
函数BulkConfirm()
{
风险值形式=$(“#份额”);
$.ajax({
键入:'POST',//获取
url:'@url.Action(“BulkUpdateConfirmation”,“Home”),
数据:form.serialize()
});
$(“#myModal”).modal('hide')
}
//单行更新
var-RowId=0;
var-tagvalue=0;
函数UpdateRow(id)
{
tagvalue=$(“#TagVaule"+id).val();
RowId=id;
displaymodel();
}
函数displaymodel()
{
$.ajax({
键入:“获取”,
url:'@url.Action(“更新”、“主页”),
数据:{
id:RowId,
值:tagvalue
},
成功:功能(数据)
{
$('#myModalContent').html(数据);
$('myModal').modal('show');
},
});
}
函数确认()
{
$.ajax({
类型:“POST”,
url:'@url.Action(“保存更新”,“主页”),
数据:{
id:RowId,
值:tagvalue
},
});
$(“#myModal”).modal('hide')
}
}
//控制器代码
公共操作结果更新(列表标记)
{
foreach(标签中的变量项)
{
如果(item.TagValue==null | | Regex.Matches(item.TagValue.ToString(),@“[a-zA-Z]”)。计数>0)
{
返回部分视图(“无效模型”);
}
}
if(ModelState.IsValid)
{
obj.updateDatas=新列表();
foreach(标签中的变量项)
{
var tag=db.Tags.Where(x=>x.Id.Equals(item.Id)).FirstOrDefault();
if(tag.TagValue!=item.TagValue)
{
UpdateData changedRow=新的UpdateData
{
OldTagValue=tag.TagValue,
NewTagValue=item.TagValue,
TagName=tag.TagName
};
对象更新数据添加(changedRow);
}
}
}
返回PartialView(“\u UpdateConfirmationBulk”,obj);
}
我以两种方式更新表单,一种是单行更新,另一种是多行更新或批量更新
@section Scripts{
<script>
$(document).ready(function () {
$('#myTable').DataTable({
'columns': [
null,
null,
null,
{ "width": "20%" },
null
],
});
});
var form = $("#share");
$(document).on('click', '#bulkupdate',function () {
debugger;
$.ajax({
type: 'GET',
url: '@Url.Action("BulkUpdate", "Home")',
data: form.serialize(),
success: function (data) {
$('#myModalContent').html(data);
$('#myModal').modal('show');
}
});
});
//BulkUpdate Confirmation
function BulkConfirm()
{
var form= $("#share");
$.ajax({
type: 'POST', //GET
url: '@Url.Action("BulkUpdateConfirmation", "Home")',
data: form.serialize()
});
$("#myModal").modal('hide')
}
//Single row update
var RowId = 0;
var tagvalue = 0;
function UpdateRow(id)
{
tagvalue = $("#TagVaule_" + id).val();
RowId = id;
DisplayModal();
}
function DisplayModal()
{
$.ajax({
type: "GET",
url: '@Url.Action("Update","Home")',
data: {
id: RowId,
value: tagvalue
},
success: function(data)
{
$('#myModalContent').html(data);
$('#myModal').modal('show');
},
});
}
function Confirm()
{
$.ajax({
type: "POST",
url: '@Url.Action("SaveUpdate","Home")',
data: {
id: RowId,
value: tagvalue
},
});
$("#myModal").modal('hide')
}
</script>
}
//Controller Code
public ActionResult BulkUpdate(List<Tag> tags)
{
foreach (var item in tags)
{
if (item.TagValue == null || Regex.Matches(item.TagValue.ToString(), @"[a-zA-Z]").Count > 0)
{
return PartialView("_InvalidModel");
}
}
if (ModelState.IsValid)
{
obj.updatedDatas = new List<UpdatedData>();
foreach (var item in tags)
{
var tag = db.Tags.Where(x => x.Id.Equals(item.Id)).FirstOrDefault();
if (tag.TagValue != item.TagValue)
{
UpdatedData changedRow = new UpdatedData
{
OldTagValue = tag.TagValue,
NewTagValue = item.TagValue,
TagName = tag.TagName
};
obj.updatedDatas.Add(changedRow);
}
}
}
return PartialView("_UpdateConfirmationBulk", obj);
}