繁体   English   中英

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

[英]Asp.NET MVC : Error while trying to call delete controller method

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

@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调用失败了,为什么?

任何人都可以建议出什么问题吗

我确定您不需要在OnSuccess和OnFailure调用中传递data

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

那些JavaScript应当通过以下方式声明data

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" });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM