use*_*444 2 jquery asp.net-mvc-3 kendo-ui
我有一个控制器动作,我想通过jquery调用更新.该操作会运行,但参数中没有数据.
我在一个列中使用带有自定义命令的kedoui网格,我想运行一些服务器代码.
kendoui grid in view
...
columns.Command(command =>
{
command.Custom("ToggleRole").Click("toggleRole");
});
...
Run Code Online (Sandbox Code Playgroud)
该模型的类型为List <_AdministrationUsers>.
public class _AdministrationUsers
{
[Key]
[ScaffoldColumn(false)]
public Guid UserID { get; set; }
public string UserName { get; set; }
public string Role { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的toggleRole脚本:
<script type="text/javascript">
function toggleRole(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
alert(JSON.stringify(dataItem));
$.ajax({
type: "POST",
url: '@Url.Action("ToggleRole", "Administration")',
data: JSON.stringify(dataItem),
success: function () {
RefreshGrid();
},
error: function () {
RefreshGrid()
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是我的控制器动作:
[HttpPost]
public ActionResult ToggleRole(string UserID, string UserName, string Role)
{
...
}
Run Code Online (Sandbox Code Playgroud)
触发控制器操作,但任何参数中都没有数据.
我将警报放在javascript中以验证"dataItem"变量中确实存在数据.以下是警报文本的样子:
{"UserID":"f9f1d175...(etc.)","UserName":"User1","Role","Admin"}
Run Code Online (Sandbox Code Playgroud)
您是否尝试在ajax post中指定dataType和contentType?
$.ajax({
type: "POST",
url: '@Url.Action("ToggleRole", "Administration")',
data: JSON.stringify(dataItem),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
RefreshGrid();
},
error: function () {
RefreshGrid()
}
});
Run Code Online (Sandbox Code Playgroud)