在 viewData MVC 中传递列表

Passing list in viewData MVC

我正在尝试使用页面名称构建一个列表以创建面包屑。我在我的布局中使用此代码:

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

问题是我在 .Add

行出错

System.NullReferenceException

编辑: 从那以后我尝试过的是

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

问题是我每次只得到实际页面

以下对我有用。请原谅我使用c#。

结果:

型号:

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

控制器:

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);
    }
}

查看:

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

这是我想出的:

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