I was creating Blogs with ASP.NET MVC 3. I have created the View that list blog post while clicking the Blog navigation link. I have also tried to list the Blog category in Layout to appear the blog category list to every link. The problem is that when I click the blog category list link it shows the runtime error like “Object reference not set to an instance of an object”. in this code of _Layout page.
@foreach (Tag tag in (IEnumerable<Tag>)ViewBag.tag)
{
<a href="@Href("~/Posts/Tags/" + tag.Name)"> @tag.Name </a>
}
In Controller of Blogs I have passed the data through ViewBag.
IEnumerable<Tag> tags = from tag in model.Tags select tag;
ViewBag.tag = tags;
Here I have also copy this code to every controller of navigation link Controller for passing data to Layout so that blog category list could appear in all link page.As this is againsts ‘DRY’.
Would you plz help me so that each blog category link reach to its corresponding category blog.
I implemented tag categories a little differently on my blog.
I created a folder which I called
HtmlHelpersinside of my project. Inside of this I have a class calledDisplayHelpers.cswhich contains code like this.Now from within your
_Layout.cshtmlpage, call this function like this:Now your tags should appear wherever you call this function!
On a side note, could I convince you to embed the anchor tags in an unordered list? Since it acts as a “Collection” of tag links, putting them in a list helps to organize them into single “entity”.