Asp.net mvc 3 奇怪的剃刀巢穴foreach(…){@Html….}
我想在我的Razor视图中这样做:Asp.net mvc 3 奇怪的剃刀巢穴foreach(…){@Html….},asp.net-mvc-3,razor,nested,Asp.net Mvc 3,Razor,Nested,我想在我的Razor视图中这样做: @foreach (Customer cust in Model.Customers) { <tr data-custid="@customer.GetIdAsText()"> @Html.RenderPartial("CustomerListTableRow", cust) </tr> } 据我所知,这应该行得通。foreach块包含一个。。。。在这里面,它处于标记模式,所以我需要@切换到C模式。问题
@foreach (Customer cust in Model.Customers)
{
<tr data-custid="@customer.GetIdAsText()">
@Html.RenderPartial("CustomerListTableRow", cust)
</tr>
}
据我所知,这应该行得通。foreach块包含一个。。。。在这里面,它处于标记模式,所以我需要@切换到C模式。问题在于,显然变量cust丢失了其类型信息和值?,因此它抱怨无法将void强制转换为object,这是RenderPartial所期望的
我让它像这样工作:
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust);
@:</tr>
}
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust); @:</tr> }
@Html.Partial("CustomerListTableRow", cust)
但是除了看起来恶心之外,如果我让VS美化代码,它会像这样把它弄糟:
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust);
@:</tr>
}
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust); @:</tr> }
@Html.Partial("CustomerListTableRow", cust)
很好,是吗-
那么,为什么第一个解决方案不起作用呢?我该怎么写呢?我想这应该行得通
@foreach (Customer cust in Model.Customers)
{
<tr data-custid="@customer.GetIdAsText()">
@Html.RenderPartial("CustomerListTableRow", cust);
</tr>
}
取决于您正在运行的MVC版本
您能提供有关错误的更多详细信息吗?我认为这应该行得通
@foreach (Customer cust in Model.Customers)
{
<tr data-custid="@customer.GetIdAsText()">
@Html.RenderPartial("CustomerListTableRow", cust);
</tr>
}
取决于您正在运行的MVC版本
您能提供有关错误的详细信息吗?是一个void方法,因此必须用{}块将其括起来:
你一定要找的是,它返回一个,它可以这样使用:
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust);
@:</tr>
}
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust); @:</tr> }
@Html.Partial("CustomerListTableRow", cust)
是一个void方法,因此必须用{}块将其封闭:
你一定要找的是,它返回一个,它可以这样使用:
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust);
@:</tr>
}
@foreach (Customer cust in Model.Customers)
{
@:<tr data-custid="@customer.GetIdAsText()">
Html.RenderPartial("CustomerListTableRow", cust); @:</tr> }
@Html.Partial("CustomerListTableRow", cust)
看看保罗的回答,这是正确的。我刚刚发现了自己,打算回到这里回答我自己的问题,但他抢先回答了我-看看保罗的回答,这是正确的。我刚刚发现了自己,打算回到这里回答我自己的问题,但他抢先回答了我-