cdemott33
asked on
ASP.NET MVC 5: Getting
Hi - I need help. I'm new to MVC development and RESTful API architecture and could use some help.
Here's an an example of my URL:
www.mysite.com/customer/details/1
In the details View I want to make a GET AJAX call to my web API that passes that "1" parameter to the GET method.
How do I pass that parameter (1) to the VIEW to call the API to GET the data?
Here's an an example of my URL:
www.mysite.com/customer/details/1
In the details View I want to make a GET AJAX call to my web API that passes that "1" parameter to the GET method.
How do I pass that parameter (1) to the VIEW to call the API to GET the data?
ASKER
Sorry if my question was unclear.
The routing to "www.mysite.com/customer/details/1" is handled by the customer controller, but in the details View, I want to call the GET function in the Customer API controller and pass the "1" as a parameter in the API GET call. Does this make sense? Or is this a totally wrong approach?
The routing to "www.mysite.com/customer/details/1" is handled by the customer controller, but in the details View, I want to call the GET function in the Customer API controller and pass the "1" as a parameter in the API GET call. Does this make sense? Or is this a totally wrong approach?
It depends on how your MVC has been setup.
Usually a URL like www.mysite.com/customer/details/1 is calling a controller customer, view details parameter 1 so I am not sure what you are asking here?
Usually a URL like www.mysite.com/customer/details/1 is calling a controller customer, view details parameter 1 so I am not sure what you are asking here?
Are you saying that you want to either:
- Make a call to a different REST API from the Customer 1 Detail after the content renders in the browser?
- Make a call to a different REST API from within the CustomerController.Details
(int id) function?
ASKER
Let me try and better explain.
I need to get a distinct customer and display his or her details on the webpage. To do this I need the parameter that is provided in the query string. (ie. www.mysite.com/customer/details/1) In my case the parameter is the number 1.
My MVC Controller only returns the view. It does not return data.
My API Controller does the work on querying the database.
On my view page I have my AJAX code. Here's a snippet:
The line I marked above is where I'm stuck. I need to pass the parameter of "1" to the Customer API Controller so I can return that individual customers data.
How can I pass that parameter to the URL line so instead of this...
url: '/api/customers/',
... it passes this...
url: '/api/customers/1',
I need to get a distinct customer and display his or her details on the webpage. To do this I need the parameter that is provided in the query string. (ie. www.mysite.com/customer/details/1) In my case the parameter is the number 1.
My MVC Controller only returns the view. It does not return data.
public ActionResult Details()
{
return View();
}
My API Controller does the work on querying the database.
public IHttpActionResult GetCustomer(int Id)
{
var customer = _context
.Customers
.Include(c => c.IntakeType)
.Include(c => c.Segment)
.SingleOrDefault(c => c.Id == Id);
if (customer == null)
return NotFound();
return Ok(Mapper.Map<Customer, CustomerDto>(customer));
}
On my view page I have my AJAX code. Here's a snippet:
$.ajax({
url: '/api/customers/', <--- THIS LINE HERE.
dataSrc: "",
type: "GET",
data: {}
}).done(function (data) {
The line I marked above is where I'm stuck. I need to pass the parameter of "1" to the Customer API Controller so I can return that individual customers data.
How can I pass that parameter to the URL line so instead of this...
url: '/api/customers/',
... it passes this...
url: '/api/customers/1',
This is where I am confused why not just make the code in your view
You can also do
Basically the question is - why can't you simply just add the number to this line
var id = <%= CLIENTID HERE%>;
$.ajax({
url: '/api/customers/' + id, <--- THIS LINE HERE.
dataSrc: "",
type: "GET",
data: {}
}).done(function (data) {
Or code it directly into the URL stringYou can also do
var data = {id: <%= CLIENTID HERE%>}
$.ajax({
url: '/api/customers/', <--- THIS LINE HERE.
dataSrc: "",
type: "GET",
data: data
}).done(function (data) {
Assumes your controller is looking for a url of controller/view?id=1 as an alternativeBasically the question is - why can't you simply just add the number to this line
url: '/api/customers/', <--- WHY CAN'T YOU JUST ADD IT HERE AS PART OF THE RENDER
ASKER
Sorry, but I'm just very new to this so please bare with me.
I would love to implement what your suggesting. My question has to do with, how do you get this...
<%= CLIENTID HERE%>;
Should I pass the parameter value from the MVC Contoller via a ViewModel to the View like this...
and then inject that value into my AJAX code like this?
Is this the best way to approach this? Again, I'm a newbie so I'm just looking for best practice for my situation.
I would love to implement what your suggesting. My question has to do with, how do you get this...
<%= CLIENTID HERE%>;
Should I pass the parameter value from the MVC Contoller via a ViewModel to the View like this...
public ActionResult Details(int id)
{
var vm = new DetailsViewModel {
Id = id
};
return View(vm);
}
and then inject that value into my AJAX code like this?
$.ajax({
url: '/api/customers/' + @Model.Id,
dataSrc: "",
type: "GET",
data: data
}).done(function (data) {
Is this the best way to approach this? Again, I'm a newbie so I'm just looking for best practice for my situation.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you for your help. This puts me on the right path.
You are welcome.
If you want to call this
Open in new window
Then just call it ...Open in new window
What am I missing?