c# - Passing a parameter through AJAX but displayed null in the MVC method -
i new mvc , i'am trying display method ajax. problem parameter passing method display null, when debug ajax code parameter not null. don't know i'm doing wrong.
this c# method
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 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); }
and 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>"; (var = 0; < 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 request."); } }); };
try changing
var params = '{"accountid":"' + accountid + '"}';
to
var params = {accountid: accountid };
and see if helps
update: didn't expect complain function... granted when ever use jquery ajax methods use specific 1 need rather root ajax() method. try maybe.
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>"; (var = 0; < 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 request."); }); };
Comments
Post a Comment