rex
rex

Reputation: 13

pass id with jquery load

i have this following jquery to load partial view in a div

 var acc = this.id;    
$('#details').load('/AccountAndTransaction/Grab', function (html)
            { $('#details')[0].value = html; });

and its working good , but now i want to pass 'var acc' with this load method , how can i achiev this??

controlller

 [HttpGet]
    public ActionResult Grab(string AccountNumber)
    {

        TermDepositAccountViewModel tdParent = new TermDepositAccountViewModel();
        account ac = db.accounts.Single(u => u.accountNumber == AccountNumber);
        deposittd td = db.deposittds.Single(u => u.accountNumber == AccountNumber);
        tdParent.deposittd = td;
        tdParent.account = ac;
        return PartialView("TermDepositPartialView", tdParent);
    }

Upvotes: 0

Views: 1125

Answers (2)

Thulasiram
Thulasiram

Reputation: 8552

     //var AccountNumber=   $(this).attr('id');  // try this

     $('#details').load('/AccountAndTransaction/Grab', {
          AccountNumber:AccountNumber} function (html){   
          $('#details:first').html(data); });
          //Or

          var AccountNumber = $(this).attr('id');
          $.ajax({
                url: '/AccountAndTransaction/Grab', type: 'Get', dataType: 'json',
                data: { AccountNumber: AccountNumber },
                success: function (data) {
                     $('#details:first').html(data);
                }
          });
     }

Upvotes: 1

Prashanth Thurairatnam
Prashanth Thurairatnam

Reputation: 4361

You just need to pass the acc as part of the URL. Since you have named your parameter in the Action method as AccountNumber it should be in this format.

$('#details').load('/AccountAndTransaction/Grab?AccountNumber=' + acc, function (html)

if your parameter name is id you can have it in this format (as in the default route the optional parameter is mapped as id, it should be automatically handled)

$('#details').load('/AccountAndTransaction/Grab/' + acc, function (html)

Upvotes: 3

Related Questions