我正在尝试将小型mvc2应用程序转换为mvc3 razor语法。在我的mvc2应用程序中,我使用带有母版页的aspx视图引擎。按照Steven Sanderson的Pro MVC2第2版的例子,在母版页中我调用了一个控制器动作,为每个实体呈现局部视图。这工作正常。
<div id="categories">
<% Html.RenderAction("Menu", "Nav"); %>
</div>
使用_layout.cshtml和razor我正在尝试这个。这就是我的问题所在。
<div id="categories">
@{
Html.RenderAction("Menu", "Nav");
}
</div>
这导致了一个无限循环,而且我得到了一个奇怪的StackOverflowException。任何人都可以帮我纠正这个问题吗?这是控制器方法代码。
public ViewResult Menu(string personId)
{
Func<string, NavLink> makeLink = pId => new NavLink
{
Text = pId ?? "Home"
, RouteValues = new RouteValueDictionary(new { controller = "Person", action = "Person"})
};
List<NavLink> navLinks = new List<NavLink> {makeLink(null)};
Func<Person, NavLink> makeLink2 = p => new NavLink
{
Text = p.Name ?? "Home"
, RouteValues = new RouteValueDictionary(new { controller = "Person", action = "Person", personId = p.Id })
};
var people = usersRepository.People.OrderBy(x => x.Name);
var peopleLinks = EnumerableHelpers.MakeLinks(people, makeLink2);
navLinks.AddRange(peopleLinks);
return View("_menu", navLinks);
}
非常感谢任何帮助或提示。
谢谢,
〜在圣地亚哥
答案 0 :(得分:23)
您没有发布实际的堆栈跟踪,但是从描述中我猜测您的recurssion是在运行布局页面的“部分”操作视图中,这会呈现操作,从而呈现布局等。
尝试从您的子操作方法而不是PartialView
返回View
。这将阻止执行_ViewStart页面,这将阻止为您的子操作呈现布局。关于这方面的更多讨论在这里:http://forums.asp.net/t/1624687.aspx
答案 1 :(得分:3)
把
@{
Layout = string.Empty;
}
在您的部分视图顶部。
答案 2 :(得分:1)
首先,你的personId参数从未使用过(无论如何都是正确路由的)?
但我肯定会从查看EnumerableHelpers.MakeLinks开始 它是隐藏递归问题的最佳位置尝试在那里设置断点
因为我使用的是
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MvcApplication1.Controllers
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class EnumerableHelpers
{
internal static List<NavLink> MakeLinks(IOrderedEnumerable<Person> people, Func<Person, NavLink> makeLink2)
{
var retVal = new List<NavLink> ();
foreach (var item in people)
{
retVal.Add(makeLink2(item));
}
return retVal;
}
}
public class usersRepository
{
private static List<Person> people = new List<Person>();
public usersRepository()
{
}
public static List<Person> People
{
get
{
people = new List<Person>() {
new Person() { Id = 1, Name = "carley" },
new Person() { Id = 2, Name = "mark" },
};
return people;
}
set
{
people = value;
}
}
}
public class NavLink
{
public System.Web.Routing.RouteValueDictionary RouteValues { get; set; }
public string Text { get; set; }
}
}
查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MvcApplication1.Controllers
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class EnumerableHelpers
{
internal static List<NavLink> MakeLinks(IOrderedEnumerable<Person> people, Func<Person, NavLink> makeLink2)
{
var retVal = new List<NavLink> ();
foreach (var item in people)
{
retVal.Add(makeLink2(item));
}
return retVal;
}
}
public class usersRepository
{
private static List<Person> people = new List<Person>();
public usersRepository()
{
}
public static List<Person> People
{
get
{
people = new List<Person>() {
new Person() { Id = 1, Name = "carley" },
new Person() { Id = 2, Name = "mark" },
};
return people;
}
set
{
people = value;
}
}
}
public class NavLink
{
public System.Web.Routing.RouteValueDictionary RouteValues { get; set; }
public string Text { get; set; }
}
}
控制器
public ViewResult Menu(string id)
{
Func<string, NavLink> makeLink = pId => new NavLink
{
Text = pId ?? "Home"
,
RouteValues = new RouteValueDictionary(new { controller = "Person", action = "Person" })
};
List<NavLink> navLinks = new List<NavLink> { makeLink(null) };
Func<Person, NavLink> makeLink2 = p => new NavLink
{
Text = p.Name ?? "Home"
,
RouteValues = new RouteValueDictionary(new { controller = "Person", action = "Person", personId = p.Id })
};
var people = usersRepository.People.OrderBy(x => x.Name);
var peopleLinks = EnumerableHelpers.MakeLinks(people, makeLink2);
navLinks.AddRange(peopleLinks);
return View(navLinks);
}
呈现
文本答案 3 :(得分:1)
尝试这样做(而不是RenderAction)
@Html.Action("Menu", "Nav")
菜单视图中有什么?有什么东西可以导致递归吗?