【发布时间】:2026-01-10 01:55:01
【问题描述】:
我已经实现了可排序的 JQuery,它运行良好。问题是我无法以新顺序将列表传递给控制器,因此我可以保存它。
如果原始列表看起来像这样 1 - 饼干 2 - 巧克力 3 - 饼干 4 - 糖果
而用户拖拽成为
1 - 巧克力 2 - 饼干 3 - 糖果 4 - 饼干
如何将新订单传递给我的控制器,以便对模型中的数据重新排序。
$('td, a', '#MenuItem').each(function () {
var cell = $(this);
cell.width(cell.width());
});
$(document).ready(function () {
$('#MenuItem tbody').sortable({
axis: 'y',
update: function (event, ui) {
var data = $(this).sortable('serialize');
// POST to server using $.post or $.ajax
$.ajax({
data: data,
type: 'POST',
url: 'MoveFunction'
});
}
});
});
[HttpPost]
public ActionResult MoveFunction(Product vm)
{
return View(vm);
}
public class Product
{
//ID's
[Key]
public int ProductID { get; set; }
//Members
<fieldset class="fieldset">
<legend class="legend">Products</legend>
<table id = "MenuItem" class="promo full-width alternate-rows" style="text-align: center;"> <!-- Cedric Kehi DEMO CHANGE -->
<tr>
<th>Product Code
</th>
<th>Product Template
</th>
@*<th>
@Html.DisplayNameFor(model => model.IndexList[0].Priority)
</th>
<th>
@Html.DisplayNameFor(model => model.IndexList[0].WindowProduct)
</th>*@
<th>Description <!-- JACK EDIT -->
</th>
<th>Actions</th>
</tr>
<tbody>
@foreach (var item in Model.IndexList)
{
<tr>
<td class="center-text">
@Html.DisplayFor(modelItem => item.ProductCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductTemplate.Description)
</td>
@*<td class="center-text">
@Html.DisplayFor(modelItem => item.Priority)
</td>
<td class="center-text">
@Html.Raw(item.WindowProduct ? "Yes" : "No")
</td>*@
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td class="center-text nowrap">
@Html.ActionLink(" ", "Edit", new { id = item.ProductID }, new { title = "Edit", @class = "anchor-icon-no-text edit" })
@Html.ActionLink(" ", "Details", new { id = item.ProductID }, new { title = "Details", @class = "anchor-icon-no-text details" })
@Html.ActionLink(" ", "Delete", new { id = item.ProductID }, new { title = "Delete", @class = "anchor-icon-no-text delete" })
</td>
</tr>
}
</tbody>
</table>
【问题讨论】:
-
由于某种原因它不允许我更新代码
-
你能举个例子说明你将如何解决它
-
我已经编辑了代码
-
我是 ajax 新手,为什么我将这个问题发布到这里以获得一些帮助,我确实有一个模型在我的视图中“@model BLL.ViewModels.Generic.GenericIndexByPromotionVM
”。我希望用户立即订购和提交,但不知道该怎么做
标签: jquery asp.net-mvc-4 jquery-ui jquery-ui-sortable