我正在尝试使用页面名称构建一个列表,以便创建面包屑。我在我的布局中使用了这段代码:
<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>
问题是我每次都只得到实际的页面
答案 0 :(得分:0)
以下对我有用。请原谅我使用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>
答案 1 :(得分:0)
这就是我提出的:
<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>