FREELessons: 22Length: 5.5 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.1 Editing and Deleting Tags

Managing our tags will be fairly straightforward. In this lesson, we'll create our tag controller and its views, and we will provide the basic functionality. To save time, we will not be writing tests.

3.1 Editing and Deleting Tags

Different content management systems, handle categories in different ways. Some have categories, some have tags, and even others have both categories and tags. Well, I want to keep our system as simple as possible. So we are going to implement only tags. And we will extend that simplicity to the implementation itself. If you go to the post class, we're going to add a property, the type will be an IList of string. And the name of the property is going to be Tags. And that is basically going to be our implementation. We will have a post, and it will contain its tags, so there's no relationship that we have to worry with, no primary or foreign keys. This is it. You can't get much simpler than this. And the great thing is that this is going to work with just about every type of data store. Now you can make the argument, that it isn't fully optimized for relational database systems. And yeah, you would be correct, but it would still work if we chose SQL server, or some other relational database as our data store. And even though we don't have a dedicated entity for a tag, we still need some way to retrieve tags from our data store. So let's go to our solution explorer, and let's add a new interface inside of our data folder. And we will call it I post repository, or not post repository, we already have one of those, don't we. We want ITag repository. And this is going to look somewhat similar to what we have in our post repository, in that we will have a method to retrieve all of the tags. I don't think we'll need one to retrieve an individual tag, because if we already have that tag, then we already have that tag. We will need to edit, and we will need to delete, and while I'm looking here, we don't have the ability to delete a post. And we will need to add that later on. But there's some other things that we will need to do inside of the post controller, so we can do that whenever we go back there. So let's go to our ITagRepository. Let's mark this as public. And let's define a method called GetAll. It's going to return an IEnumerable of string. And this is going to be used inside of our tag controller. So let's go to our solution explorer. Let's right click on Controllers, and let's add a new controller. Let's use the empty template, and we will call it TagController. Now, because we are going to be working with our tags, we need a private field for our repository. So private read only ITagRepository. We need a using statement for our data namespace. Lets call this _repository. I believe that's what we called it, inside of our post controller and, it is. So that is going to be consistent. We also need to inject this dependency, so so we will do that through the constructors, so we will add a public TagController. The parameter will be ITagRepository, and we'll just call this repository. And then inside we will set our private field, equal to the repository object that was passed as the parameter. So inside of our index action method, we want to retrieve all of our tags. So let's call this variable tags, and we will call our repository's GetAll method. And then we will pass those tags to the view. So let's right click and add a view. We want to call this Index. Let's choose the list template, because that is going to automatically generate a table for us. It will also give us the links for editing and deleting. So this is going to go a long way towards giving us what we want. However, the model class we cannot use a string here. You can see that the add button is disabled, and that is really annoying, because in order to get this to work, we have to choose something other than a string. We can't do IEnumerable of string, unfortunately, so we are stuck using something from our drop down. So, if we choose a post, that's going to allow us to create the view, but we're also going to have to modify that view. Now we don't want this as a partial view, so lets uncheck that. So let's change the layout. Actually, no, we want the admin layout. So let's click on add. This is going to generate our markup for us, but as you can see it give us a lot of extra stuff that we don't need. So, let's first of all change our model. We no longer have an IEnumerable of posts. We have and IEnumerable of string. And then we can get rid of this create link, because we aren't going to create our tags here. We're going to do that inside of the forum for creating and editing posts. So, we will get to that later on. So, we can get rid of this link. And then we just need to clean up this table. So we can get rid of most of these columns. We need the first column, because that's going to display the tag itself. So let's change the heading here to simply Tag Name. And then we need this other column, that does not have a heading. Because this is going to contain the links for editing and deleting tags, so we can leave this one column here. We can get rid of everything else except the final column, because this gives us our edit and delete link. We don't need the details, so we can get rid of that. And let's change this to simply displaying the item within our model. We also need to change these expressions down here. Instead of, we simply want to set id equal to item. And actually, let's change this so that it is tag, instead of id. And that should be it for this view. So now let's go back to our tag controller, and let's add an edit method, one that will handle get requests. So HttpGet, and public, ActionResult, Edit, and we want to accept the tag. Now, we need some way of insuring that a tag, that is passed to the edit method, does indeed exist. So let's go back to our tag repository. And let's add a method that will tell us that information. So it will return a boolean, and let's just call it Exists, and then we will pass in the tag. So we can go back to our tag controller. Let's first of all, check to see if our tag exists. So if not, repository.Exsists. Passing in the tag, then we want to return HttpNotFound. Otherwise, we want to pass that tag on to the view. So we will return view, and then passing in the tag. So let's Let's right click. Let's add a view. We want to call it Edit, and let's choose the Edit template. Now once again we can't use string here as the model class, so we're going to have to pick something. Let's once again choose post, we are going to have to edit our form then. Let's click on add, and we are going to have to modify basically everything here, because nothing is really going to work, except for the AntiForgeryToken, And the validation summary. So, first of all, let's change the model from a post to a string, and for the hidden field we want one, because we need to keep track of the tag that we are editing. So, let's use the hidden method. Let's give it the name tag and the value is going to be our Model. And then for the form fields, we want just one field. So we can get rid of everything else. We of course need our submit button, but everything else is gone. And we aren't going to be using these methods. We probably will for the text box, but for the label it will be easier just to use label for, and we can call this, new tag. And we can also assign the classes here. So, let's just grab this. Let's cut and paste, and that will give us our label. So, then we can we can get rid of this call to Html.LabelFor, and then we don't need the EditorFor method, although we could use the text box, so we can use Html.TextBox. And let's give this a name of newTag, and the value is going to be our model. So we will pass that there. And we won't have the validation message either, we will just rely upon the validation summary. So this is basically all that we should have to do. So we can go back to our controller now, and let's write the edit method for post requests. So let's copy what we have, and paste it. Let's change the attribute to HttpPost. We also need to validate the AntiForgeryToken, and we need another parameter, we have our tag, but we also need the new tag. And we should also check to see if our new tag exists, because if it does, then we don't want to do anything. So if repository exists, passing in newTag, then we can redirect, back to our index. So, RedirectToAction and index. Although, this isn't very efficient, because we are hitting our data store for what is essentially the same thing. We are checking to see if a tag exists. So we can make this better. We can do Var tags = _repository.GetAll. And then we can use the Contains method. So instead of using our repository, we'll just use our tags and then the Contains method. And that will basically give us the same thing, but it will execute better. Now we probably still want to keep our Exists method because it works really well here. Otherwise, we would have to retrieve all of our tags. And do what we did with the Contains method, and I don't really want to write all those lines of code just for one tag. So we'll keep our Exists method, but we will be sure to do this pattern, whenever we need to check for multiple tags. Okay, so we have the checks for, if the tag doesn't exists, we have the check for if the newTag does exist, now we need to actually edit. So we can call our repository, and Edit. We need first of all the tag that we are editing. And then the new value, so newTag. And we need the edit method on our repository so let's, control dot, and enter to add that. Let's go back to our repository interface, and that works, although let's change this to newValue. Well, no, let's not. Let's call it newTag, and then we will call the other parameter existingTag. That way both parameter names, are descriptive as to what they are. Okay, so we have our edit methods for both get and post requests. Oh, although in this case we do not want to return the view. We should redirect to our index. And really before we call Edit we should ensure that our newTag is an actual value, so if string.isNullOrWhitSpace, we'll pass in newTag, then this is an error. So we can add an error to our ModelState, so we will call the AddModelError. The key we'll just use as key, and then for the error, The new tag value cannot be empty. And then we need to return the view, passing in our existing tag. So that should cover all of our bases. Now we need the form for deleting, so we can use the same thing that we did for our edits, so let's copy all of that, and we will paste it in. We, of course, need to change the names here. So lets do delete. We'll use the same logic, and then for the other one, delete. We no longer need the new tag, because we aren't doing anything except deleting this tag. So, we can get rid of the check, for the new tag, if it exists, and here we could use our exists method. So, instead of using our tags variable, we can do _repository and then Exists. We no longer need our tags variable, and instead of calling the edits method on our repository, we will call Delete, passing in tag, and that will be it. And we of course we don't have this on our repository so control dot enter, and let's make sure that everything looks good in our interface, and it does. Now we don't necessarily need a view for deleting, but let's go ahead and create one. Because, in my mind, we wouldn't do all of the deleting through JavaScript, we could pop up an alert box asking, hey, do you want to delete this? And then if the user clicks on yes, then we can make an Ajax call. But let's go ahead and lets add the delete view, and that is basically going to be our edit view. So we can copy and paste that view, but we need to change it to delete. Let's change the title of this view to delete as well. And there's really nothing else that we need to do here, although we no longer need this text box. So we can just get rid of that. Let's change the value of the button to delete. Oh and we have a heading here of post, let's change that to tag. And for the h2 heading let's do Delete and then our model. That way we can see the model on the screen. And that should do it for our tag controller. Now we do need a way of creating tags. And, as I've already mentioned, we will be doing that inside of the forums for creating and editing posts. And we will do that in the next lesson.

Back to the top