繁体   English   中英

没有从Ajax调用MVC控制器方法

[英]MVC controller method is not being called from ajax

MVC控制器方法未从我已声明的Ajax中调用。 PFB代码片段C#控制器:

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

尝试这个:

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

如今最好使用Promise,如果要返回json,最好返回JsonResult而不是ActionResult

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

  1. 正确建立网址:

    $ .ajax({type:'POST',cache:false,url:' @ Url.AbsoluteAction(“ PhoenixInbox”,“ Checkfunction”) ',

  2. 确保您允许获取Get操作: JsonRequestBehavior.AllowGet

    公共ActionResult Checkfunction(字符串ReqID,字符串AssociateId,字符串AssetId){MyDetails obj = new MyDetails(); List Lst =新的List(); Lst = objMyAssetsDetails.Check(AssociateId,AssetId,ReqID); 返回this.Json(Lst,“ text / json”,JsonRequestBehavior.AllowGet); }

暂无
暂无

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

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