ElChapo ElChapo - 3 months ago 15
Vb.net Question

Passing list in viewData MVC

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

Answer

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>
Comments