FREELessons: 20Length: 2.3 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.9 Creating Dummy Data

Hello, and welcome back to, MVC for designers. Now that we've created our product model, we're going to take that model and create some dummy data with it in our shopping controller. Now once the developers take over this project, this dummy data that we're about to create is no longer going to be used. Instead they're going to be pulling it from the database. But we want to temporarily simulate some database data so that we can actually look through that data in our view. To display our products on the screen. And we're doing all of this in the hopes that we can make our developers' lives easier once they start taking over the project and working on our files. So I wanna start again by creating a new copy of our store project, and we're gonna call it Store07. We'll jump into that folder and open up that solution, so all of our changes that we make in this lesson will be saved in that new folder. All right, so we've got our project here, we can open up our models and it looks like I didn't save it before I copied it so let's go back over here. Let's delete that and copy it again. Now that it's saved, we should have all of our data in place, or all of our models. And let's try that again. So once that opens up we should see. There we go. Now are models folder is populated and we have our product.cs file. So what I want to do is I want to create some dummy data. So we're going to create some products each of which will have a name, a price, a description and a product ID. And we're going to do that in our controller, because our controller is kind of the link between our model and our view. So we're going to be doing a little bit more coding in this lesson, but again, we're going to keep it pretty simple. So we're going to the ShoppingController, and here we have our index action. This index action right now is just returning a view, but we can also pass in a model. And first we need to populate that model, and I want this model to be a list of products. So the way we create a list of products is we first type in the data type we're going for, which is list of products, so we're gonna type list. And then inside angle brackets, we need to put the type of data that's gonna be stored in this list. Well the type that we're using is the product model that we just created or the product class we just created. So inside these angle brackets I'm gonna type in the word product and you'll see that that word, that product word doesn't show up in our code hinting, and when we hover over it, we can see that we're getting an error here. Well if we click on the word product, you'll see there's a little blue box underneath the the first letter there, and, if we hover over that box, we get this little pop-up. And, if we click on this down arrow on this little pop-up, we see that we can add a using statement here. And, we're gonna add this statement using store dot models. Basically what's going on here, if we just type in the word product, our code doesn't know what to do with that. It doesn't automatically know that we have a file called product.cs somewhere on this site. But if we add this using statement. And I'm just gonna click on that, and you'll see that it adds it up here at the very top. And if you don't wanna add it that way, you could just type in what you see up here, using store.models. So, store is the name of our project, as you can see here. Models is the name of the folder that contains product.cs. So, all this is saying is we wanna look inside store.models. And if we refer to any of the class names that we see in that folder, then we should know what to do with them. Such as right here and you'll notice that when we added that using statement, the text for the word product turned from white to green and that's usually a good sign so we're gonna create a list of products. Make sure there's no space after the word list and before the angle bracket there. So, that's our data type. Now, we're gonna give it a name. So, this is just basically just a variable that we're creating. So, I'm gonna type space and then I'm gonna give it a name and I'm just gonna call it call it model. Our model for this action, for this particular view that we're going to be using is going to be a list of products. So, we're gonna call it model and for now we're going to set it equal to a new list of products. And then after your closing angle bracket there, we need to put opening and closing parentheses. So basically what we're doing is we're creating a new list of products, and we're storing it inside this variable called model. Everything before the word model here is basically just telling us what type of data we're storing in this variable. And then again, over here to the right of the equals sign, that's where we're actually creating that list. Okay, so we've created a list of products, we've stored it in this variable called model. Now we can start populating that model. So the way I'm gonna do that, there's a couple of ways we could do it, but the way I'm gonna do it is I'm gonna point to model. And then we were using a list we can type .add. And then inside parenthesis, we can add a new item. So the way I'm gonna do this, you'll notice I've got my cursor inside the parenthesis right now. I'm gonna type new Product, and then opening and closing curly brackets. With my cursor inside the curly brackets, I'm gonna hit the Enter key, and then I'm gonna hit the down arrow key to put my cursor after the closing parenthesis. And I'm gonna type a semicolon to end that statement, this model.add statement. And then in between these curly brackets here I'm gonna type in all the data we need for this particular product. So our first product is beard wax. So remember, we created some properties in our model. Called product name, product price, product description and product ID. These are the items we are going to be setting values for now. So since Visual Studio knows what is stored in this product model, we can just start typing in. I'll just type in the letter p, and it gives us a list of those properties we created. So I'm going to go down to product name and we're going to set that equal to a value. We're going to set it equal to "Beard wax". And that needs to be in quotation marks and then we can type a comma, go down to the next line and set our next property equal to something. Here we're gonna do product price and remember we set this up as a string, so we're gonna go ahead and include all the formatting. So inside quotation marks again I'm going to type $. Then let's say it's $5 comma. Then we're gonna have a product description. We're going to set that equal to a value as well. I'm just going to copy and paste some lorem ipsum text here. I'll paste that right there and then after the closing quotation make there we'll hit a comma and go to the next slide and then finally. We'll go to product ID, and this is actually a integer, so we're just going to enter a number with no quotation marks, so we'll set this as product ID of 1. And that's all we need to do to create a dummy product. And by using model.add, it adds this new product that we've created to our list, which is called model. So, we're just going to do this a few more times. We'll just copy this. And, I'll paste it three times. And so, for our second one, instead of beard wax we're going to have a tree axe, which is going to be, let's say, $25, and we'll use that same lorem ipsum text and just change the product ID to 2. Our third item is going to be a flannel shirt. A high quality flannel shirt which costs $45 and we'll give it a product id of 3. Obviously, we would change the product description too. But I'm just using dummy text anyway, so it doesn't really matter. And then for our final item here, we're going to sell some suspenders. And let's say that those suspenders cost $8.50, and we'll give this a product ID of 4. So now that we've added these four products to this model that we created up here, we now need to pass that model into our view. And it's actually easy to do. We just go down here where it says return view and we just pass in model. So, if we get everything hooked up properly, then we should be able to access all the data in this model from within our view. And we'll get to that in the next lesson. For now, let's just save our file and then we'll move on. Thank you for watching.

Back to the top