在post上发送JQuery变量
我无法解决我的Jquery问题。。 我一直在四处寻找,找到了一些解决方案,但它们似乎不起作用 我有一个MVC项目,在一个视图中,我使用的是sortable,它工作起来很有魅力。 问题是当我想将列表的新顺序发送回控制器时。。 我正在使用在post上发送JQuery变量,jquery,asp.net-mvc,post,Jquery,Asp.net Mvc,Post,我无法解决我的Jquery问题。。 我一直在四处寻找,找到了一些解决方案,但它们似乎不起作用 我有一个MVC项目,在一个视图中,我使用的是sortable,它工作起来很有魅力。 问题是当我想将列表的新顺序发送回控制器时。。 我正在使用alert(items)并显示我想要的字符串 @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Arran
alert(items)
并显示我想要的字符串
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>ArrangeAttributesViewModel</legend>
<div id="contentWrap">
<p>
</p>
<p>
</p>
<div>
_category.CategoryName</div>
<div id="attributeList" class="container">
<ul>
@foreach (var _category in Model.AttributeList)
{
<li id="@("a_" + _category.CategoryID.ToString())">
@_category.CategoryName
</li>
}
</ul>
</div>
<p>
</p>
<p>
<input type="submit" id="SaveButton" value="Save Changes" />
</p>
</div>
</fieldset>
}
在控制器中
非常感谢任何帮助
编辑:
在一些用户输入后,我对代码做了一些更改
现在一切正常,直到发送变量为止,换句话说,当我按下按钮时,我会看到警报…您的变量“items”超出了$.post的范围。在.sortable()之外声明变量
var项;// 好的,我解决了这个问题
首先,我将控制器的输入定义为字符串[]
,这是不正确的。它应该是一个列表
其次,我将$.post
更改为$.ajax
,这就是我最终得到的结果(忽略变量名、items到itemIds的更改):
希望这对其他人有用 谢谢,我还意识到我没有使用右键单击事件。。不幸的是,它仍然不能解决我的问题,我将编辑问题。。。
<script type="text/javascript">
$(document).ready(function () {
$("li").hover(function () {
$(this).css('cursor', 'move');
$(this).css('background-color', '#2c87b2');
},
function () {
$(this).css('cursor', 'auto');
$(this).css('background-color', '#5c87b2');
});
$("#attributeList ul").sortable({
opacity: 0.6,
cursor: 'move',
update: function () {
items = $(this).sortable('toArray');
alert(items);
}
});
var items;
$("#SaveButton").click(function () {
alert(items);
$.post({
url: '/Profile/ArrangeCategories',
type: 'POST',
data: { items: items },
traditional: true
});
});
});
</script>
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult ArrangeCategories(int ProfileID, string[] items)
var items; // <-- declare the variable here.
$("#attributeList ul").sortable({
opacity: 0.6,
cursor: 'move',
update: function () {
items = $(this).sortable('toArray');
alert(items);
}
});
$('submit').click(function () {
$.post({
url: '/Profile/ArrangeCategories',
type: 'POST',
data: { items: items }
});
});
$("#SaveButton").click(function () {
var order = $("#attributeList ul").sortable('toArray');
alert(order);
$.ajax({
data: { itemIds: order },
type: 'POST',
traditional: true
});
});