从JavaScript调用服务器端JsonResult方法

时间:2013-07-16 01:06:21

标签: asp.net-mvc asp.net-mvc-4 extjs ext.net

我需要调用以下JsonResult方法:

JsonResult Delete(int pubId)
{
    try
    {
        using (var ctx = new LibsysLiteContext())
        {
            var p = ctx.Publishers.Find(pubId);
            var allPublisher = ctx.Publishers.ToList();
            ctx.Publishers.Remove(p);
            var total = allPublisher.Count();
            return Json(new { success = true, data = allPublisher, total = total }, JsonRequestBehavior.AllowGet);
        }
        return Json(new RestResult { Success = true, Data = entity, Message = "Country  has been deleted" }, JsonRequestBehavior.DenyGet);
        return null;
    }
    catch (Exception e)
    {
        return Json(new RestResult { Success = true, Message = e.Message }, JsonRequestBehavior.DenyGet);
    }
}

来自js函数(deleteRows):

var deleteRows = function () {
    Ext.Msg.confirm(
        'Delete Rows', 'Are you sure?',
        function(btn) {
            if (btn == 'yes') {
                var hh = Ext.getCmp('gg').deleteSelected();
                ajax({
                    //action and controller
                    url: '@Url.Action( "Publisher", "Delete")',
                    data: { "Id": Id },
                    type: 'POST',
                    dataType: 'json',
                });
            }
        });
};

由以下按钮的处理程序调用:

X.Button().ID("bntdelete").Text("delete").Icon(Icon.Delete).Handler("deleteRows();"),

这一切都不起作用!如何从JavaScript函数从客户端移动到服务器端?

2 个答案:

答案 0 :(得分:0)

通常从js调用服务器端[Direct Method],您使用App.direct.<Method>();

答案 1 :(得分:0)

希望您现在已经解决了这个问题,但是在您的ajax调用中,您将类型定义为post,但是您在控制器上显示的内容被设置为默认get。你需要添加

[HttpPost]
public JsonResult Delete(int id)...

到您的控制器