簡體   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