TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Nicholas Mabe
NA
72
4.7k
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:
public
IActionResult Details(
int
Id)
{
var accountFromDb = _unitOfWork.Account.
GetFirstOrDefault(u => u.AccountId == Id, includeProperties:
"Country"
);
var RequestId = Id;
return
View(accountFromDb);
}
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:
[HttpGet]
public
IActionResult DetailsByAccount(
int
id)
{var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == id);
return
Json(
new
{ data = proposalFromDb });
}
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:
[HttpGet]
public
IActionResult DetailsByAccount(
int
id)
{var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == 3,);
return
Json(
new
{ data = proposalFromDb });
}
I’ve tried many ways to try and get the ID into the API call, for example:
public
IActionResult DetailsByAccount(
int
Id)
{var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == RequestId);
return
Json(
new
{ data = proposalFromDb });
}
I’ve tried this:
public
IActionResult DetailsByAccount(
int
Id)
{
Id = RequestId
var proposalFromDb = _unitOfWork.Proposal.GetAll(u => u.AccountId == Id);
return
Json(
new
{ data = proposalFromDb });
}
On the datatable it sends the following call:
function
loadDataTable() {
dataTable = $(
'#tblData'
).DataTable({
"ajax"
: {
"url"
:
"/Admin/Account/DetailsByAccount"
},
If I change it to:
function
loadDataTable() {
dataTable = $(
'#tblData'
).DataTable({
"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.
Reply
Answers (
2
)
Printing form gives me blank page from printer
What is the difference between Debug and Release Build Mode in Visual