I am creating a breadcrumb partial view which takes in a collection of title/URL. The collection will be generated in action methods and would have to be available on the breadcrumb partial view.
I tried couple of ways to get it done and this is one among such: http://goo.gl/rMFlp
But some how i could not get it working. All i get is an “Object reference not set to an instance of an object.” Can you guys help me out?
{Updare}
Here is the code:
I created a Model class as follows
public class ShopModel
{
public Dictionary<string,string> Breadcrumb { get; set; }
}
Action Method
public ActionResult Index()
{
var breadcrumbCollection = new Dictionary<string,string>();
breadcrumbCollection.Add("/home","Home");
breadcrumbCollection.Add("/shop","Shop");
var model = new ShopModel() { Breadcrumb = breadcrumbCollection};
return View(model);
}
Model binding the view – Index
@Model NexCart.Model.Model.Custom.ShopModel
Finally here is the code on partial view:
<div>
@{
foreach (var item in @Model.Breadcrumb)
{
<a href="#">@item.Key</a>
}
}
You haven’t shown any code, so your question is impossible to answer. This being said here’s how you could proceed. As always in an ASP.NET MVC application you start by defining a view model:
then you could write a controller action which will populate a collection of breadcrumbs and pass them to a partial view:
then you could have a corresponding partial view which will render this model (
~/Views/Breadcrumb/Index.cshtml):and the corresponding display template (
~/Views/Breadcrumb/DisplayTemplates/Breadcrumb.cshtml):Now all that’s left is to include this child action somewhere using the Html.Action helper. For example you could do this in the _Layout if this breadcrumb is repeated on each page:
But obviously it could also be done in any view.