Link to home
Create AccountLog in
Avatar of cdemott33
cdemott33Flag for United States of America

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?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I am not clear on the requirement
If you want to call this
var url="www.mysite.com/customer/details/1"

Open in new window

Then just call it ...
$.get(url, function(resp){
  // handle response
});

Open in new window


What am I missing?
Avatar of cdemott33

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?
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?
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?
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.  

public ActionResult Details()
        {
            return View();
        }

Open in new window


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));
        }

Open in new window


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) {

Open in new window


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
var id = <%= CLIENTID HERE%>;
$.ajax({
                url: '/api/customers/' + id,   <--- THIS LINE HERE.
                dataSrc: "",
                type: "GET",
                data: {}
            }).done(function (data) {

Open in new window

Or code it directly into the URL string
You can also do
var data = {id: <%= CLIENTID HERE%>}
$.ajax({
                url: '/api/customers/',   <--- THIS LINE HERE.
                dataSrc: "",
                type: "GET",
                data: data
            }).done(function (data) {

Open in new window

Assumes your controller is looking for a url of controller/view?id=1 as an alternative

Basically 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

Open in new window

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...

public ActionResult Details(int id)
        {
            var vm =  new DetailsViewModel {
                Id = id
            };
            return View(vm);
        }

Open in new window


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) {

Open in new window


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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thank you for your help.  This puts me on the right path.
You are welcome.