Asp.NET MVC:尝试调用删除控制器方法时出错

时间:2014-08-11 11:58:57

标签: c# asp.net asp.net-mvc-4 razor

这是我的剃刀代码的一部分

@using (Ajax.BeginForm("DeleteBeacon", "Beacons", new { beaconId = Model.ID, instId = Model.InstID, pageNo = (int)ViewBag.pageNumber }, new AjaxOptions { LoadingElementId = "imgloaderIndex", OnSuccess = "OnSuccessDelete(data)", OnFailure = "OnFailureDelete(data)" }))

这是我的控制器代码:

public ActionResult DeleteBeacon(UserProfile userInfo, long beaconId, long instId, int? pageNo)
    {
        bool status = false;
        int pNumber = (pageNo ?? 1);          
        ViewBag.pageNumber = pNumber;
        try
        {
            status = blBeacon.DeleteBeacon(beaconId, userInfo.UserID, userInfo.SessionToken, null);
            if (status)
            {
                return Json(new { status = "success", pageNumber = pageNo, instId = Convert.ToInt64(TempData["InstituteId"]) });
            }
            else
            {
                return Json(new { status = "failure" });
            }
        }
        catch (Exception exp)
        {
            LogUtil.Error("BeaconsController\\DeleteBeacon:\n" + exp.Message);
            return Json(new { status = "failure", message = exp.Message });
        }

    }

它在本地服务器上工作正常,但在生产服务器上托管时,我在尝试删除时遇到错误。错误是

POST https://portal.example.com/Beacons/DeleteBeacon?beaconId=36&instId=9594&pageNo=2 500 (Internal Server Error) 
Uncaught ReferenceError: data is not defined 

更新:我挖得更深一点,它显示data is not defined in OnFailureDelete(data)这意味着POST调用失败但是为什么??

任何人都可以建议可能出错的地方

1 个答案:

答案 0 :(得分:0)

我确定您不需要在OnSuccess和OnFailure电话中传递data

... { LoadingElementId = "imgloaderIndex", OnSuccess = "OnSuccessDelete", OnFailure = "OnFailureDeleteenter code here"

那些应该声明data的JavaScript,例如:。

function OnSuccessDelete(data) { //... }

编辑:

我无法复制您的问题。但是,这是我的工作示例代码,希望它可能对您有用:

<强>控制器

[HttpPost]
public ActionResult DeleteBeacon(object beaconid, object instid, int pageno)
{
    return Json(new { result = "Hello" });
}

查看

@using (Ajax.BeginForm("DeleteBeacon", "Home", new { beaconId = 1, instId = 2, pageNo = 3 }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccessDelete", OnFailure = "OnFailureDelete" }))
{
    <input type="submit" value="Click Me"/>    
}

    <script type="text/javascript">
        function OnSuccessDelete(data) {
            console.log(data);
        }

        function OnFailureDelete(data) {
            console.log("Failure " + data);
        }
    </script>

要呼叫OnFailureDelete(),您必须将代码的响应状态设置为200以外的其他内容,例如

[HttpPost]
public ActionResult DeleteBeacon(object beaconid, object instid, int pageno)
{
    Response.StatusCode = 500;
    return Json(new { result = "Hello" });
}