简体   繁体   中英

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

Here is a portion of my razor code

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

Here is my controller code:

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

    }

It works fine in local server , but when hosted on production server, I'm getting error while trying to delete. Error is

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

UPDATE: i dug a bit deeper and it shows data is not defined in OnFailureDelete(data) which means the POST call is failing but why ??

can any one suggest what may be wrong

I'm sure you don't need to pass data in the OnSuccess and OnFailure calls.

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

The JavaScript for those should have data declared though eg:

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

EDIT:

I'm unable to replicate your issue. However, here is my working example code, in the hope that it might be of some use to you:

Controller

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

View

@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>

To call OnFailureDelete() you must set the response status to code to something other than 200 eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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