FREELessons: 22Length: 5.5 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 First Steps

The content in our CMS can be represented by an individual post. In this lesson, we'll create a class to represent an individual post and flesh out our post controller for managing posts.

2.1 First Steps

We'll gets started by focusing on the content, and managing the content within our cms. Now we're not going to worry about storing data just yet, because we have plenty of time to do that. But we do need code that allows us to work with our content within our application, and we also need to start working on the user interface so that we can manage that content. So let's start by writing a class that will represent the content within our system. So let's go to our models folder, let's right-click, and let's add a new class. I'm going to call this Post, because that's somewhat generic. It could be a blog post, it could be a news post, but chances are it's going to be a post of some kind. And then we need some properties. Now the first thing we need is a unique ID, someway to identify an individual post. Now this could be somewhat determined by our data store. If we're going to store our data within a database, typically the primary key is an integer value, but if we are going to store these as files on the file system, then a string would make sense, because a filename has to be unique. But I would also make the argument that we are working with posts and a post is a resource in a web application. And you access a resource through a URL and a URL is two things. It's first of all a string and it's also unique so in our case it kind of makes sense to have a primary key that is a string. So let's make it a string and if we need to change it later on we can. Let's call it Id, and then let's create another property called Title, which will be the title of the post. We also need the Content itself and it would also be helpful to have the date and time of when the post was created. So let's give this a type of DateTime and we'll call this property Created. And in the same vein, it would be useful to have a date and time of when the post was published. Now we can make this nullable. So that if it's no, then the post hasn't been published yet. But if it does have an actual value, then it has been published. So let's call this Published. And then finally, we need to know the author of the post. Now, once again this is going to be somewhat determined by our data store. Although we could make the argument that it could be a string, because user names typically don't change. Now, we could have a customizable field which would be the display name, and the author wanted to modify that, then they could. But the user names typically don't change. Let's just leave this as an int, and once again, if we need to change that later then we can. And let's just call this AuthorId. And now that we have this Post class, let's start writing a controller that will allow us to modify and create new posts. So let's go to our solution explore. But instead of creating a controller inside of the controller's folder, let's create an admin area so that we can put everything related to the administration portion of our CMS inside of that admin area. So let's right-click on the project, and we want to add a new area. So we have that option directly within the menu, and let's call this Admin. And then inside of the Controllers folder in our Admin area, we will add this post controller. So let's add a new controller, and we will simply use the empty controller template and we will call it PostController. Now before we do anything else we need to design the URLs of our application. Now everything inside of the admin area should be off of the admin segment. So we should have /admin and that will take us to the admin dashboard which will then give us the ability to create a new post or if we need to manage users or things like that. So /admin is a given. Now because we are in the PostController, we want another segment of /post. So we will have /admin/post and then the index is going to display all of the posts within our system. And then we can have other action methods like edit and things like that. So let's start by adding a couple of attributes to our PostController. The first is the RouteArea and we want to specify Admin. Then we also want the RoutePrefix because we want everything inside of this controller to be prefixed with post. And then we have our Index action method, which will display all of our posts. But we also need a method for editing a post. So let's do public ActionResult and we'll just call it Edit, but we also need to know what post that we want to edit, so let's use the Route attribute. We want the edit prefix and then the id of the post that we want to edit, so that will be our parameter. And we will make this a string id, and let's go ahead and let's document this. So the URL will be /admin/post/edit and then post-to-edit. And let's also specify this as HttpGet. And then we can copy all of this and we can use this as the basis for the post version of the edit method. So let's just change HttpGet to HttpPost. And then we need to modify our parameter. We no longer want the string id. Instead we want our post. So we need a using statement for our model's namespace. And let's just call this model. And we can go ahead and stomp out some of this logic because we know that we want to check to see if the model is valid. So we can check to see if not model state is valid, then we want to return the view, passing back in the model. Now, I know we don't have our view yet, but this is essentially what we are going to want to do. Because if the model is not valid, then we need to send it back to the view so that the user can make it valid. Otherwise, then we need to do something, so we can add a TODO: update model in data store, and then we can redirect to someplace else. Now, because we are working with posts and we are editing a post, it kinda makes sense to go back to the index. So we can return a redirect result and we want to redirect to the action of index. And since we've stubbed out some code in the Post method, let's do the same thing inside of the get. Let's add a TODO: to retrieve the model from the data store. So let's create a dummy object for our models. So let's just new Post. And then we will return the View. Passing in the model. So we have the boilerplate for editing. Now we need the boilerplate for creating. So we can do this with a method called create. So let's add a public ActionResult. We'll call it Create. And we don't need any parameters here, but we do need our attributes. So let's just copy what we have for the edit method. Let's paste that here. And of course, instead of edit we want to create, and we don't need the id. Let's also change our comment so that it is simply /admin/post and then /create. We don't need the id in the URL there. And then we will come and do the same thing that we did inside of the get Edit method. We will create a model object, and then pass that onto the view. Then we can use this as a basis for the Post Versions. So let's copy and paste. Let's change the attribute to HttpPost and here we want a Post object to work with, which we will call model. And we basically want to do the same thing that we did inside of the edits method for a Post request. We want to check to see if the model is valid. And if it is, then we want to redirect to the index, otherwise we will send it back to the view so that the user can fix whatever needs to be fixed. Now we need to create the views for the edit and create action methods, and we will do that in the next lesson.

Back to the top