1

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.

2
  • Is the tags variable set before being passed to the ViewBag? If you were to break on the line 'ViewBag.tag = tags;' has it definitely been set with values from the model.Tags? Commented Dec 2, 2011 at 8:30
  • ya, ViewBag.tag value is set with value of model.Tags.. Commented Dec 2, 2011 at 13:07

1 Answer 1

1

I implemented tag categories a little differently on my blog.

I created a folder which I called HtmlHelpers inside of my project. Inside of this I have a class called DisplayHelpers.cs which contains code like this.

namespace ProjectName.HtmlHelpers
{
    public static class DisplayHelpers
    {
        public static MvcHtmlString GetTags()
        {
            var tags = from tag in model.Tags select tag;

            StringBuilder sb = new StringBuilder();

            foreach (Tag tag in tags)
            {
                sb.AppendFormat("<a href="{0}"> {1} </a>",
                     // link,
                     tag.Name);
            }

            return new MvcHtmlString(sb.ToString());
        }
    }
}

Now from within your _Layout.cshtml page, call this function like this:

@DisplayHelpers.GetTags()

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".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot JesseB. It was so helpful and solve my problems. As you have told, I have already use unordered list. And, There is problem using sb.AppendFormat. When I click first time to blog category link, it works but second time when I click the link, the anchor href path is appended to previous anchor href path. Is there alternative function to avoid this problems. Again, thanks for your ideas.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.