剃刀行为异常?
剃刀在跟我开玩笑。我有一个部分视图:
@model ManageMvc.Models.Default.Classes.MvcModule
@{
if (Model.CanExpand)
{
Response.Write("Crazy");
@:TEST
<text>ojiiojjiojiojiojiojiojiojio</text>
Response.Write("Crazy2");
}
else
{
Response.Write("Crazy");
@:TEST
<text>ssssssssdffffffff</text>
Response.Write("Crazy2");
}
}
这是从这里调用的:
@if (Model.Modules.Count > 0)
{
for (var i = 0; i < Model.Modules.Count; i++)
{
Html.Partial("~/Views/UserControls/_MenuItem.cshtml", Model.Modules[i]);
}
....
}
我期望 razor 打印出 Text 块和 @: 块内写入的内容。但我什么也没得到。当我运行它时,它只打印 CrazyCrazy2 (对于列表中的每个模块)。 我错过了什么吗?
已更新
调用 if (Model.Modules.Count > 0) 的代码本身就是一个部分代码。这个是从布局页面调用的。所以上面的代码是被调用的第二部分。这有什么区别吗?
布局->主菜单(部分)-> CreateMenuItem(部分)
更新
这是新代码:(Shared->DisplayTemplates内的_MenuItem.cshtml)
@model ManageMvc.Models.Default.Classes.MvcModule
@{
if (Model.CanExpand)
{
@:TEST
<text>ojiiojjiojiojiojiojiojiojio</text>
}
else
{
@:TEST
<text>ssssssssdffffffff</text>
}
}
调用菜单项的部分视图MainMenu:
@Html.DisplayFor(x => x.Modules, "_MenuItem")
现在这在该行上中断,我得到以下内容错误:
传递到字典中的模型项的类型为“System.Collections.Generic.List`1[ManageMvc.Models.Default.Classes.MvcModule]”,但是该字典需要类型为“ManageMvc.Models.Default.Classes.MvcModule”的模型项。
Razor is playing tricks with me. I have a partial view:
@model ManageMvc.Models.Default.Classes.MvcModule
@{
if (Model.CanExpand)
{
Response.Write("Crazy");
@:TEST
<text>ojiiojjiojiojiojiojiojiojio</text>
Response.Write("Crazy2");
}
else
{
Response.Write("Crazy");
@:TEST
<text>ssssssssdffffffff</text>
Response.Write("Crazy2");
}
}
This is called from this:
@if (Model.Modules.Count > 0)
{
for (var i = 0; i < Model.Modules.Count; i++)
{
Html.Partial("~/Views/UserControls/_MenuItem.cshtml", Model.Modules[i]);
}
....
}
I expected razor to print out whats written inside the Text block and the @: block. But i get nothing. When i run this it just prints CrazyCrazy2 (for each module in the list).
Did i miss something?
Updated
The code that is calling if (Model.Modules.Count > 0) is a partial itself. That one is called from the layout page. So the top code is the second partial being called. Can this make any difference?
Layout -> MainMenu (partial) -> CreateMenuItem (partial)
Updated
This is the new code: (_MenuItem.cshtml inside Shared->DisplayTemplates)
@model ManageMvc.Models.Default.Classes.MvcModule
@{
if (Model.CanExpand)
{
@:TEST
<text>ojiiojjiojiojiojiojiojiojio</text>
}
else
{
@:TEST
<text>ssssssssdffffffff</text>
}
}
Partial view MainMenu that is calling the menu item:
@Html.DisplayFor(x => x.Modules, "_MenuItem")
Now this breaks on that line and i get the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ManageMvc.Models.Default.Classes.MvcModule]', but this dictionary requires a model item of type 'ManageMvc.Models.Default.Classes.MvcModule'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调用
Html.Partial
帮助器时放置@
并避免在 ASP.NET MVC 视图中使用Response.Write
:也不要编写一些丑陋的循环我强烈建议您使用模板化助手。因此,用这个简单的助手替换布局中的 for 循环:
然后定义显示模板 (
~/Views/Shared/DisplayTemplates/MvcModule.cshtml
),该模板将为的每个元素呈现>Modules
集合:看看这有多容易吗?
Put an
@
when calling theHtml.Partial
helper and avoid usingResponse.Write
in an ASP.NET MVC view:Also instead of writing some ugly loops I would strongly suggest you using templated helpers. So replace the for loop in your layout with this simple helper:
and then define the display template (
~/Views/Shared/DisplayTemplates/MvcModule.cshtml
) which will be rendered for each element of theModules
collection:See how easier this is?