Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:10Length:1.5 hours
  • Overview
  • Transcript

4.2 Displaying a Single Object

In this lesson, you'll learn how to return 404s when a resource doesn't exist. You'll also learn that routing is something you'll constantly have to keep in mind as you write your applications.

4.2 Displaying a Single Object

In the previous lesson, we created a view for the index action method so that we would display a list of products. But we also need to be able to display an individual product. And we can go about doing that in several different ways. And we're going to examine some of those. So that you could see what they are and you could choose the best way for you. So the first thing we need to do is examine the URL because how it was originally designed isn't really conducive for displaying an individual item. The reason being because we have products followed by the collection of products, guitars in this case, followed by the action. So this means that if we wanted to view an individual guitar in this case, it would have to have some type of action for displaying an individual item. And while that would work, I don't like that. Because it should be if we have the collection of products/guitars followed by an id, well then we should just see that item. That should just be the default behavior. So we are going to swap the id and the action so that if we have just the collection of products, guitars, then we see the list of guitars. If we have the id, then we see that particular guitar. And then if we have edit, then we have the ability to edit that guitar. So id is going to come before action and this is still going to work, because action is default as index. So if we don't specify an action, index is going to be used. Now this is going to force us to do a couple of different things. The first thing we have to do is accept an id for index. Because whether if we are displaying a collection of guitars or just an individual guitar, the action that's going to be used is index because we aren't specifying an action. But in this case, id is optional. So this is going to be a nullable id. And then we will go check to see if id has a value. And if it does, then we would return a view for displaying an individual item. And there is an overload for the view method that allows us to specify the view that we want to use. So in this case, the View would be SingleProduct. And then we would pass in our model, which would be an individual guitar. So that's one option. But I personally don't like that because I like my action methods to be responsible for doing one thing and one thing only. An index action method should be just for displaying the index, the collection of products. So instead of doing this, we're going to write another action method. And how we get to that action method is going to depend upon what we want to do. So let's first of all write this. This this is going to be an IActionResult, will be called singleProduct and then it will accept an id. And this means we can get rid of that there. And then inside of single product, the first thing we will do is get the appropriate guitar. So we will use our repository and we will use the Get method. Pass in the id. Now if we don't have guitar, then it's going to be null. That means that that resource doesn't exist. So we will return a NotFound result. But if we do have a guitar, then we will return a view passing in the guitar that we have, and there we go. So let's go ahead and create this view so that we don't have to do that later. So inside of Guitars, we're going to create a new file. And we're going to call this SingleProduct.cshtml. Our model is going to be of type secondapp.Models.Guitar. And then let's just have an H1 element, we'll say @Model. Let's use the Manufacturer and we'll also have the name of the guitar model. So we'll use the model and then also we'll have the year of that guitar. So there's our view. It's very simple but we are at least displaying some information here. So now we need to decide how we are going to execute, or route our request to this single product action method. So let's go back to our routing table. Now the first thing that we could do is just create a new route. And we would call this product single-route. Now we want to define this one before our just products route. When you define your routes, you want to do so in the order of the most specific to the most generic. Because if the products route was before this product single route, then our URL of products/guitars/10 would be caught by this route. Because the id would be present and the action would default to index and we don't want that. So this is a very specific URL. This is still a specific URL but it's not as specific as this other one. So we have specific and then somewhat specific and then not specific at all. So now this is going to allow us to route our request to this single product. But we need to tell it the action that we want. And we can do that by passing in another argument to map route. This is called defaults and it's an object of where we set the default values for our parameters. So if we wanted to set a default value for controller, we could do that. Although we wouldn't want to do that because we want this product single-route to work for all of our products. So we could set the id as well but once again, we don't want to do that. But in our case, we do want to set action. Now even though action is not defined inside of our URL, we can still use action here because action is a built-in name in the MVC framework. So action is always going to represent an action method and here we can say SingleProduct. Now I'm using the exact casing, but you don't have to. You can use it all lowercase or all uppercase. It doesn't matter. It's going to match that particular method. So our route here uses the URL template of products. And then the controller and then the id. It's going to default the action to single product and that should work. So let's go to the browser, and let's make a request for just guitars. This should give us our list of guitars, it does. If we say guitars/1, then we get the individual guitar. Now, if we say guitars/1/edit, we get the old response of the guitar id, and then 1. So the edit is still working or at least the products route is working. So in this lesson, we created a view as well as defined a new route for displaying an individual product. And the point that I want to drive home is that it doesn't matter how many routes you have. If you find that you need a route for a specific purpose, define a route.

Back to the top