2015-10-08 42 views
0

MVC控制器方法未從我聲明的ajax中調用。 PFB的代碼片段 C#控制器:未從ajax調用MVC控制器方法

public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId) 
{ 
     MyDetails obj = new MyDetails(); 
     List<string> Lst = new List<string>(); 
     Lst = obj.Check(AssociateId, AssetId, ReqID); 
     return this.Json(Lst, "text/json"); 
} 

Javascript代碼(AJAX調用):闖民宅細節控制器和的WebMethod Checkfunction

$.ajax({ 
    type: 'GET', 
    cache: false, 
    url: '@Url.Action("Details/Checkfunction")', 
    data: { 'ReqID': RequestId, 'AssociateId': AssociateID, 'AssetId': Host_Name }, 
    contentType: "application/json", 
    success: function (data) { 
     debugger; 
     if (data.length > 0) { 

       ViewModel.REQUESTID() = data[0]; 
       ViewModel.FLAG() = '1'; 
     } 
     else { 
      debugger; 
      ViewModel.FLAG() = '0'; 
      ViewModel.REQUESTID() = ''; 
     } 

     if (ViewModel.REQUESTID() != '' || ViewModel.REQUESTID() != null) { 
      debugger; 
      ViewModel.REQID() = RequestId; 
     } 
    }, 

    error: function (error) { 
     alert("error"); 
    } 
}); 
+2

Checkfunction是不是在你的ajax調用? – g2000

+1

您的ajax網址指向'PhoenixInbox/CheckMSDNRemap',但您的方法是'Checkfunction'?這將是一個很好的起點 – Jonesopolis

+1

GetUrl()是做什麼的? – ssimeonov

回答

0

現在它最好使用承諾,如果你要返回JSON,它更好的回報JsonResult代替的ActionResult

http://davidwalsh.name/write-javascript-promises

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Nullify

+0

是的,你是對的,我今天剛剛註冊,我有幾件事要學習;) – ChaosPattern

+0

是的。使用JsonResult工作。非常感謝。 :) – Sherin1589

1

試試這個:

$.ajax({ 
type: 'POST', 
cache: false, 
url: '/PhoenixInbox/Checkfunction', 
data: { 'ReqID': RequestId, 'AssociateId': AssociateID, 'AssetId': Host_Name }, 
contentType: "application/json", 
success: function (data) { 
    debugger; 
    if (data.length > 0) { 

      ViewModel.REQUESTID() = data[0]; 
      ViewModel.FLAG() = '1'; 
    } 
    else { 
     debugger; 
     ViewModel.FLAG() = '0'; 
     ViewModel.REQUESTID() = ''; 
    } 

    if (ViewModel.REQUESTID() != '' || ViewModel.REQUESTID() != null) { 
     debugger; 
     ViewModel.REQID() = RequestId; 
    } 
}, 

error: function (error) { 
    alert(JSON.stringify(error)); 
} 
}); 

控制器:

[Httppost] 
public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId) 
{ 
     MyDetails obj = new MyDetails(); 
     List<string> Lst = new List<string>(); 
     Lst = objMyAssetsDetails.Check(AssociateId, AssetId, ReqID); 
     return this.Json(Lst, "text/json"); 
} 
+0

'/ PhoenixInbox/Checkfunction'可能會在生產中炸燬,如果應用程序未部署在根目錄中。所以這個url應該在運行時用'Url.Action'來構建。 –

0
  1. 構建正確的網址:

    $阿賈克斯( { 類型: 'POST', 緩存:假, URL: '@ Url.AbsoluteAction( 「PhoenixInbox」, 「Checkfunction」)',

  2. 確保您允許獲得獲取動作:JsonRequestBehavior.AllowGet

    公衆的ActionResult Checkfunction(字符串REQID,串AssociateId,串由assetid) { MyDetails的obj =新MyDetails(); List Lst = new List();Lst = objMyAssetsDetails.Check(AssociateId,AssetId,ReqID); return this.Json(Lst,「text/json」,JsonRequestBehavior.AllowGet); }

相關問題