【发布时间】:2018-11-06 20:18:27
【问题描述】:
每当用户根据自己的偏好对表行进行排序时,我都会尝试使用 ajax 发送任务 ID。但似乎在 ajax 中捕获的数据并未发送到控制器。我已经调试了代码并且数据是在 jQuery 中捕获的,但是 ajax 不发送它。可能是什么问题?这是我的代码。 我的任务索引
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
@section scripts {
<script type="text/javascript">
$("#sortable").sortable({
update: function (event, ui) {
var itemIds = "";
$("#sortable").find(".taskSingleInline").each(function ()
{
var itemId = $(this).attr("data-taskid");
itemIds = itemIds + itemId + ",";
});
$.ajax({
url: '@Url.Action("UpdateItem", "TaskBoard")',
data: { itemIds: itemIds },
type: 'POST',
success: function (data) {
},
error: function (xhr, status, error) {
}
});
}
});
</script>
}
<style>
#sortable tr:hover {
background-color: cadetblue;
color: beige;
}
</style>
@using (Html.BeginForm("Index", "TaskBoard", FormMethod.Post))
{
<div class="container">
<div class="col-md-6">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
</tr>
</thead>
<tbody id="sortable" style="cursor:pointer;">
@foreach (var i in Model)
{
<tr>
<td>@i.Id</td>
<td class="taskSingleInline" id="task@(i.Id)" data-taskid="@(i.Id)">@i.Name</td>
<td>@i.RowNo</td>
</tr>
}
</tbody>
</table>
<button type="submit"> submit</button>
</div>
</div>
}
这是我的控制器
public ActionResult UpdateItem(string itemIds)
{
int count = 1;
List<int> itemIdList = new List<int>();
itemIdList = itemIds.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
foreach( var i in itemIdList)
{
try
{
First f = TE.Firsts.Where(x => x.Id == i).FirstOrDefault();
f.RowNo = count;
TE.Firsts.AddOrUpdate(f);
TE.SaveChanges();
} catch(Exception)
{
continue;
}
count++;
}
return Json(true, JsonRequestBehavior.AllowGet);
}
TF 是我的 Db 上下文 我的模型“第一” enter image description here
我已经在我的共享布局中渲染了
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
【问题讨论】:
-
控制器中 itemIds 变量的值实际上是 null 还是您只是说该功能不起作用?
-
我不认为我的 jquery 工作或我的控制器功能。如果这就是您所要求的,那么 itemids 不为空。
-
我刚刚删除了我的 try-catch 块并得到了这个异常 System.InvalidOperationException 属性“RowNo”是对象的关键信息的一部分,不能修改。
-
问题出在你的控制器/模型而不是 jquery 上。确保您的 Firsts 表中的主键设置正确。
-
谢谢……是的,问题出在我的第一张桌子上。我修好了
标签: jquery ajax asp.net-mvc jquery-ui-sortable