我有一个区域问题,并从他们生成链接.这是我正在使用的代码的粗略结构:
Home
Area1
Area 1 Content
Area2
Area 2 Content
Area3
Area 3 Content
在我的_layout.cshtml文件中,我生成一个菜单(完全由表驱动):
foreach (MainMenu mm in parentMenus)
{
List<SubMenu> theseChildren = childMenus.Where(o => o.MainMenuId == mm.MainMenuId).OrderBy(p => p.Ordering).ToList();
result.Append(String.Format(@"<h3><a href='#'>{0}</a></h3>", mm.Name));
result.Append(String.Format(@"<div>"));
result.Append(String.Format(@"<p>"));
foreach(SubMenu sm in theseChildren){
//Issue is here:
result.Append(String.Format(@"<a href='{0}/{1}/{2}'>{3}</a> <br />", sm.AreaName == null ? String.Empty : sm.AreaName, sm.ControllerName, sm.ActionName, sm.Name));
}
result.Append(String.Format(@"</p>"));
result.Append(String.Format(@"</div>"));
}
Run Code Online (Sandbox Code Playgroud)
这是因为它是为手风琴(jQuery)生成的.
所以,问题出在foreach循环中.当代码在"Home"区域中运行时,它很好,但是当它在本地区域外运行时,它会产生奇怪的结果.
所以,例如,我在数据库中有一个记录OPS.它应该创建一个指向OPS/OPS/INDEX的链接(area = OPS,Controller = OPS,Action = INDEX).在家庭"区域",它很好,但当它在一个区域,它出来"http:// localhost:17416/Home/OPS/OPS/INDEX"
任何可以提供的帮助都会很棒!
在此先感谢大家.
您必须更改代码以在链接中指定区域,如下所示:
@Html.ActionLink("Label", "Action", "Controller", new { area = "Area" }, null)
Run Code Online (Sandbox Code Playgroud)
这应该工作:
foreach(SubMenu sm in theseChildren){
result.Append(@Html.ActionLink(sm.Name, sm.ActionName, sm.ControllerName, new { area = sm.AreaName }, null).ToHtmlString());
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助...
用这个:
String.Format(
"<a href='{0}'>some text you want</a>",
Url.Action("ActionName", "ControllerName", new { area = "AreaName" })
);
Run Code Online (Sandbox Code Playgroud)
代替:
String.Format(
@"<a href='{0}/{1}/{2}'>{3}</a> <br />",
sm.AreaName == null ? String.Empty : sm.AreaName,
sm.ControllerName,
sm.ActionName, sm.Name)
Run Code Online (Sandbox Code Playgroud)
例如:
String.Format(
"<a href='{0}'>{1}</a>",
Url.Action(sm.ActionName, sm.ControllerName, new { area = sm.AreaName }),
sm.Name
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6115 次 |
最近记录: |