Nicholas Mabe

Nicholas Mabe

  • 1.6k
  • 72
  • 4.3k

How to capture and reuse a dynamic variable in a Controller?

Jan 15 2021 12:21 PM
I’m opening a view with the following address: https://localhost:44322/Admin/Account/Details/3
 
At this point it has filtered out the account I’m looking for which has the Id 0f 3. It returns the relevant data. Also on the view is a Datatable which also needs to be filtered with the same ID.
 
The AccountControlller.cs is using the following to return the view:
  1. public IActionResult Details(int Id)  
  2. {  
  3. var accountFromDb = _unitOfWork.Account.  
  4. GetFirstOrDefault(u => u.AccountId == Id, includeProperties: "Country");  
  5. var RequestId = Id;  
  6. return View(accountFromDb);  
  7. }
With var RequestId = Id I am trying to capture the received I’d and use it with the API call that is also being sent to the AccountControlller.cs to populate the Datatable.
 
The API call is as follows:
  1. [HttpGet]  
  2. public IActionResult DetailsByAccount(int id)  
  3. {var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == id);  
  4. return Json(new { data = proposalFromDb });  
  5. }  
It does not return any data. If I insert breakpoints and check the Id is 0.
 
If I set an ID I get a the return I’m looking for, for example:
  1. [HttpGet]  
  2. public IActionResult DetailsByAccount(int id)  
  3. {var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == 3,);  
  4. return Json(new { data = proposalFromDb });  
  5. }
I’ve tried many ways to try and get the ID into the API call, for example:
  1. public IActionResult DetailsByAccount(int Id)  
  2. {var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == RequestId);  
  3. return Json(new { data = proposalFromDb });  
  4. }  
I’ve tried this:
  1. public IActionResult DetailsByAccount(int Id)  
  2. {  
  3. Id = RequestId  
  4. var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == Id);  
  5. return Json(new { data = proposalFromDb });  
  6. }  
On the datatable it sends the following call:
  1. function loadDataTable() {  
  2. dataTable = $('#tblData').DataTable({  
  3. "ajax": { "url""/Admin/Account/DetailsByAccount" },  
If I change it to:
  1. function loadDataTable() {  
  2. dataTable = $('#tblData').DataTable({  
  3. "ajax": { "url""/Admin/Account/DetailsByAccount?Id=3" },
I get all the related records for the Account with the ID of 3, just what I’m looking for.
 
I’ve tried everything I can find to get the dynamic variable into the url string but the only thing I’ve got is a headache.
 
It seems the recordset can be filtered in either the AccountController or in the Datatables script, but I just can’t work out
 
1. how to share the var RequestId created in public IActionResult Details(int Id) with the API call in the same controller
[HttpGet] public IActionResult DetailsByAccount(int id)
 
2. How to capture the dynamic value in the URL ( in this case “3”) and add it dynamically to "ajax": { "url": "/Admin/Account/DetailsByAccount" },
 
Any help is greatfully received.

Answers (2)