0

I am trying to build a list with the name of the pages in order to create a breadcrumb. I am using this code in my layout:

<ol class="breadcrumb " style="margin-bottom: 0px;">
    @ViewData("Chemin") As New List(Of String)
    @If (Not IsDBNull(ViewContext.RouteData.Values("action").ToString())) Then
    @ViewData("Chemin").Add(ViewContext.RouteData.Values("action").ToString())
    End If
    @For i = 0 To ViewData("Chemin").Count - 1
    @:<li>
        @ViewData("Chemin").Item(i)
        @:</li>
    Next i
</ol>

the problem is that I get an error on the line with .Add

System.NullReferenceException

EDIT : What I've tried since is

<ol class="breadcrumb " style="margin-bottom: 0px;">

                                    @If (Not IsDBNull(ViewData("Chemin"))) Then
                                        @Code Dim lst As New List(Of String)
                                            ViewData("Chemin") = lst
                                        End Code
                                            End If

                                    @If (Not IsDBNull(ViewContext.RouteData.Values("action").ToString())) Then
                                        @Code ViewData("Chemin").Add(ViewContext.RouteData.Values("action").ToString()) End Code
                                            End If

                                    @For i = 0 To ViewData("Chemin").Count - 1
                                        @:<li>
                                            @ViewData("Chemin").Item(i)
                                            @:</li>
                                    Next i
</ol>

The problem is that I get each time only the actual page

ElChapo
  • 228
  • 3
  • 13
  • Were you able to solve this? – poppertech Jun 28 '16 at 16:37
  • No, I'll update my question with what I've tried since. Are you trying to do the same ? edit: that being said the error I had was because you need to replace the second line with: @Code ViewData("Chemin") As New List(Of String) End Code – ElChapo Jun 29 '16 at 09:27
  • How does my solution differ from what you want to accomplish? – poppertech Jun 29 '16 at 11:48
  • I want to use only razor in my layout, moreover it doesn't really answer the question as it's an alternative, which is good by the way – ElChapo Jun 29 '16 at 12:00
  • When you say "I want to use only razor in my layout," do you mean that you want to use ViewData instead of a view model? If not, then which lines of my solution are not acceptable? – poppertech Jun 29 '16 at 12:10
  • Actually the right way would be with session variable. Comparing to the use of a session variable, is your solution a better way and why ? – ElChapo Jun 29 '16 at 12:22
  • One of the advantages of MVC is it is stateless (http://stackoverflow.com/questions/102558/biggest-advantage-to-using-asp-net-mvc-vs-web-forms). This becomes particularly important if the application is deployed on the cloud. – poppertech Jun 29 '16 at 15:16
  • The advantages of a strongly typed view are: 1) Automatic scaffolding; 2) Intellisense support; 3) Compile time type checking (https://blogs.msdn.microsoft.com/rickandy/2011/01/28/dynamic-v-strongly-typed-views/) – poppertech Jun 29 '16 at 15:18
  • More on the "stateless" nature of MVC: http://stackoverflow.com/questions/8525920/is-asp-net-mvc-stateless – poppertech Jun 29 '16 at 15:28

2 Answers2

0

The following worked for me. Please excuse my use of c#.

Result:

ResultImage

Model:

public class Chemin
{
    public List<string> RouteValues { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        Chemin chemin = new Chemin();
        chemin.RouteValues = Url.RequestContext.RouteData.Values.Select(v => v.Value.ToString()).ToList();
        return View(chemin);
    }
}

View:

@model passing_list_in_viewdata_mvc.Models.Chemin

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

<ol class="breadcrumb">

    @for (int i = 0; i < Model.RouteValues.Count; i++)
    {
        <li>@Model.RouteValues[i]</li>
    }

</ol>
poppertech
  • 1,286
  • 2
  • 9
  • 17
0

This is what I've come up with :

<ol class="breadcrumb " style="margin-bottom: 0px;margin-top: 10px;">
     @If Session IsNot Nothing Then
       @If ((Session("Chemin") Is Nothing) Or (ViewContext.RouteData.Values("action".ToString()) = "Index")) Then
                                            @Code Dim lst As New List(Of String)()
                                                Session("Chemin") = lst
                                            End Code
                                                End If
                                        @Code Dim values = ViewContext?.RouteData?.Values End Code
                                        @If (values IsNot Nothing) And (Not (ViewContext.RouteData.Values("action") Is Nothing) AndAlso Not Session Is Nothing AndAlso Not (Session("Chemin") Is Nothing) AndAlso Not Session("Chemin").Contains((ViewContext.RouteData.Values("action").ToString()))) Then
                                            @Code Session("Chemin").Add(ViewContext.RouteData.Values("action").ToString())
                                            Session("Chemin").Add(ViewContext.RouteData.Values("controller").ToString())
                                                End Code
                                            End If

                                        @For i = 0 To Session("Chemin").Count - 2 Step 2
                                            @:<li>
                                            @:<a href =@Url.Action(Session("Chemin").Item(i), Session("Chemin").Item(i + 1))>
                                            @Session("Chemin").Item(i)
                                            @:</a>

                                                @:</li>
                                        Next i
   End If
</ol>
ElChapo
  • 228
  • 3
  • 13