Why is $('#sortthis').sortable("serialize") just returning a string that says '[object Object]'?
I'm trying to get an ajax version of sortable working. I have this Javascript:
<script src="/Scripts/jquery-ui/jquery.ui.widget.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui/jquery.ui.mouse.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui/jquery.ui.sortable.js" type="text/javascript"></script>
<script type="text/javascript">
// Sortable
$(document).ready(function () {
$("#sortThis").sortable({
handle: '.handle',
update: function () {
// get new order
var order = $('#sortthis').sortable('serialize');
// excecute ajax for db update
开发者_如何学编程 $.post(
"/find/ajaxactionhere",
order,
function (data) {
$("#info").html(data);
}
);
}
});
});
</script>
And this html in my asp.net mvc view:
<table>
<thead>
<tr>
<th>headers</th>
<th>headers</th>
</tr>
</thead>
<tbody id="sortThis">
<% foreach (var item in Model) %>
<% { %>
<tr id="list_<%: item.Tier %>">
<td>
<img class="handle" src="/sortIcon.gif" />
</td>
<td><%: item.data %></td>
</tr>
<% } %>
</tbody>
</table>
This successfully allows me to reorder the all of the table rows. My action to handle the ajax post request is like this:
[HttpPost]
public string ajaxactionhere(FormCollection form)
{
StringBuilder sb = new StringBuilder();
sb.Append(":: ");
if (form != null)
{
foreach (var key in form.AllKeys)
{
sb.Append(form[key]);
}
}
else
{
sb.Append("null");
}
return sb.ToString();
}
the javascript variable order
has $('#sortthis').sortable('serialize');
assigned to it. Whenever I display order
it says "[object Object]". I can't figure out how to get the contents of that object as a string.
Any help would be greatly appreciated.
Your element's ID is sortThis
with capital T
.
$('#sortthis').sortable('serialize')
will just return the empty jQuery object to indicate that it didn't find any matching elements by that selector.
Change
var order = $('#sortthis').sortable('serialize');
to
var order = $('#sortThis').sortable('serialize');
and it should work.
精彩评论