【发布时间】:2012-04-01 23:38:36
【问题描述】:
在我的网页中,我有一系列基本上只包含信息行的表格。这些中的每一个都在 for 循环中给出了一个 id,我试图从外部引用它们。我向表格和“保存更改”按钮添加了类。
基本上,我的目标是让用户能够拖放行,从而改变顺序。然后他们可以单击“保存更改”按钮,这将与相关信息一起发回服务器。
我无法将按钮与相关表匹配,从而将每行的 id 以数组的形式提交回服务器。我编写了代码,以便能够从每个表中获取 ids 及其当前顺序,但我不知道如何通过单击 jQuery 按钮将其分配给数组。
这是视图:
@foreach (var collection in Model.Collections)
{
<h2>@collection.Season</h2>
@Html.ActionLink("Delete Collection", "DeleteCollection", new { controller = "Edit", brand = collection.Brand.Name, season = collection.Season })
@Html.ActionLink("Edit Collection", "EditCollection", new { controller = "Edit", brand = collection.Brand.Name, season = collection.Season })
@Html.ActionLink("Add Image", "CreateImages", new { controller = "Edit", season = collection.Season })
<p>
To change the ordering of images, drag and drop to your desired position and then click the Save Changes button on the appropriate collection.
</p>
<table class="table-collection" id="table-@collection.Id">
<tr class="nodrop nodrag">
<th>
Id
</th>
<th>
Description
</th>
<th>
Image
</th>
<th>
Options
</th>
</tr>
@foreach (var image in collection.Images)
{
<tr id="@collection.Id-@image.Id">
<td class="dragHandle showDragHandle">
@image.Id
</td>
<td>
@image.Description
</td>
<td>
<img src="@Url.Content("~/" + image.Location)" alt="@image.Description" />
</td>
<td>
<ul>
<li>
@Html.ActionLink("Edit", "EditImage", new { controller = "Edit", brand = image.Collection.Brand.Name,
season = image.Collection.Season, imageId = @image.Id } )
</li>
<li>
@Html.ActionLink("Delete", "DeleteImage", new
{
controller = "Edit",
brand = image.Collection.Brand.Name,
season = image.Collection.Season,
imageId = @image.Id
})
</li>
</ul>
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Save Changes" class="save-order" id="saveTable-@collection.Id"/>
</p>
}
这是到目前为止的 jQuery:
$(document).ready(function () {
$(".table-collection").tableDnD();
$(".save-order").click(function (e) {
e.preventDefault();
$.ajax({ url: window.location.href,
type: 'POST',
data: { ids: $("--ASSIGN ARRAY HERE--"
});
遍历每一行的jQuery本质上是这样的:
function(table, row) {
var rows = table.tBodies[0].rows;
var debugStr = "Row dropped was "+row.id+". New order: ";
for (var i=0; i<rows.length; i++) {
debugStr += rows[i].id+" ";
}
【问题讨论】:
-
您要在数据中使用 json 格式发布吗..?
标签: c# jquery ajax model-view-controller