0

我是 MVC 的新手,我正在尝试使用 AJAX 显示方法。问题是我传递给方法的参数显示为空,但是当我调试 ajax 代码时,参数不为空。我不知道我做错了什么。

这是我的 C# 方法

 public JsonResult AllAccountList(int accountID)
{
    Client myClient = new AccountServiceClient().GetClientByUsername(System.Web.HttpContext.Current.User.Identity.Name);

    IEnumerable<Account> myAccounts = new AccountServiceClient().GetAccountWithClient(myClient.ClientID);
    List<AccountModel> myList = new List<AccountModel>();

    foreach (Account a in myAccounts.Where(a => a.AccountID == Convert.ToInt32(accountID)))
    {
        myList.Add(new AccountModel() { AccountID = a.AccountID,TypeID_FK = a.TypeID_FK, FriendlyName = a.FriendlyName, Currency = a.Currency, AvailableBalance = a.AvailableBalance});
    }

    return Json(myList, JsonRequestBehavior.AllowGet);
}

这是我的 AJAX

function btnSelectClicked(AccountID) {
            var params = '{"accountID":"' + AccountID + '"}';
            $.ajax({
                type: "POST",
                url: "/Account/AllAccountList",
                data: params,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var gridview = "<table>";
                    gridview += "<tr><td id='titleCol'>AccountId</td><td id='titleCol'>Account Type</td><td id='titleCol'>Friendly Name</td><td id='titleCol'>Currency</td><td id='titleCol'>Available Balnce</td></tr>";
                    for (var i = 0; i < data.length; i++) {
                        gridview += "<tr><td>" + data[i].AccountID +
                                    "</td><td>" + data[i].TypeID_FK +
                                    "</td><td>" + data[i].FriendlyName +
                                    "</td><td>" + data[i].Currency +
                                    "</td><td>" + data[i].AvailableBalance + "</td></tr>";
                    }
                    gridview += "</table>";
                    $("#display2").html(gridview);
                },

                error: function (xhr,err) {
                    alert(xhr.responseText + "An error has occurred during processing your request.");
                }
            });
        };
4

1 回答 1

1

尝试改变

var params = '{"accountID":"' + AccountID + '"}';

var params = {accountID: AccountID };

看看是否有帮助

更新:我没想到会抱怨一个函数......当我使用 jquery ajax 方法时,我使用的是我需要的特定方法,而不是根 ajax() 方法。试试这个也许。

function btnSelectClicked(AccountID) {
    var params = {accountID: AccountID };
    $.post("/Account/AllAccountList", data, function(data) {
        var gridview = "<table>";
            gridview += "<tr><td id='titleCol'>AccountId</td><td id='titleCol'>Account Type</td><td id='titleCol'>Friendly Name</td><td id='titleCol'>Currency</td><td id='titleCol'>Available Balnce</td></tr>";
            for (var i = 0; i < data.length; i++) {
                gridview += "<tr><td>" + data[i].AccountID +
                            "</td><td>" + data[i].TypeID_FK +
                            "</td><td>" + data[i].FriendlyName +
                            "</td><td>" + data[i].Currency +
                            "</td><td>" + data[i].AvailableBalance + "</td></tr>";
            }
            gridview += "</table>";
            $("#display2").html(gridview);
        });
    })
    .fail(function(jqXHR, textStatus, errorThrown ){
        alert(jqXHR.responseText + "An error has occurred during processing your request.");
    });
};
于 2013-04-05T14:04:41.520 回答