我应该把这段代码放在哪里?
下面是编写在控制器中的代码:
CategoryRepository categoryRepo = new CategoryRepository();
var categories = categoryRepo.FindAllCategories();
ViewBag.Categories = categories;
现在我想使用它来动态创建一个漂亮的类别列表。
<div id="leftnavigationbar">
<ul>
@foreach (var category in ViewBag.Categories)
{
//Create li here.
}
<!-- ActionLink goes: Text, ActionName, Controller -->
<li>@Html.ActionLink("Libros", "Index", "Home")</li>
<li>@Html.ActionLink("Peliculas, Musica & Juegos", "Index", "Anuncios")</li>
<li>@Html.ActionLink("Computadoras", "Index", "Usuarios")</li>
<li>@Html.ActionLink("Bienes Raices", "Index", "Ayuda")</li>
<li>@Html.ActionLink("Bolsa de Trabajo", "Index", "Contacto")</li>
<li>@Html.ActionLink("Deportes y Fitness", "Index", "Contacto")</li>
<li>@Html.ActionLink("Electronicos y Celulares", "Index", "Contacto")</li>
</ul>
</div>
现在我正在将此代码写入 _Layout.cshtml 文件。但我想知道在哪里编写它,以便它始终运行,有点像母版页。
有什么建议吗?
编辑:
看来我最初的意图是不可能的。
@foreach (var category in ViewBag.Categories)
{
<li>@Html.ActionLink(category.Name, "Index", "Home")</li>
}
关于如何完成我想做的事情有什么建议吗?只需提取类别列表并使用 foreach 循环呈现它们即可。然后将其放置在某个位置,以便在所有页面上都可以查看。
Here's the code, written to be written in a controller:
CategoryRepository categoryRepo = new CategoryRepository();
var categories = categoryRepo.FindAllCategories();
ViewBag.Categories = categories;
Now I'd like to use this to create a nice list of Categories dynamically.
<div id="leftnavigationbar">
<ul>
@foreach (var category in ViewBag.Categories)
{
//Create li here.
}
<!-- ActionLink goes: Text, ActionName, Controller -->
<li>@Html.ActionLink("Libros", "Index", "Home")</li>
<li>@Html.ActionLink("Peliculas, Musica & Juegos", "Index", "Anuncios")</li>
<li>@Html.ActionLink("Computadoras", "Index", "Usuarios")</li>
<li>@Html.ActionLink("Bienes Raices", "Index", "Ayuda")</li>
<li>@Html.ActionLink("Bolsa de Trabajo", "Index", "Contacto")</li>
<li>@Html.ActionLink("Deportes y Fitness", "Index", "Contacto")</li>
<li>@Html.ActionLink("Electronicos y Celulares", "Index", "Contacto")</li>
</ul>
</div>
Right now I'm writing this code to the _Layout.cshtml file. But I'd like to know where to write this so it runs always, sort of like a MasterPage.
Any suggestions?
Edit:
It appears that my initial intent isn't possible.
@foreach (var category in ViewBag.Categories)
{
<li>@Html.ActionLink(category.Name, "Index", "Home")</li>
}
Any suggestions on how accomplish what I'm trying to do? Just pull a list of categories and render them using a foreach loop. Then have it placed somewhere so it's viewable on all pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与往常一样,您首先创建视图模型:
然后在控制器中填充视图模型:
最后,强类型视图可以使用显示模板。显然,视图将被强类型化为 IEnumerable
并且您的显示模板(~/Views/Home/DisplayTemplates/CategoryViewModel.cshtml):
正如您所见,使用强类型视图和显示模板,您甚至不需要在视图中编写循环。
作为显示模板的替代方案,您可以使用
Html.Action
或Html.RenderAction
工具。 Phil Haack 写了一篇关于它们的不错的博客文章。我怀疑您被迫使用丑陋的无类型 ViewBag,因为此代码位于您的主模板中,并且您没有视图模型。没问题,使用Html.Action
您可以有一个特定的控制器来执行此操作:然后有一个相应的部分视图,它将负责呈现类别(
~/Views/Categories/ Index.cshtml
):最后使用相同的显示模板。
现在,在您的母版页中,您可以简单地包含此子操作:
As always you start by creating a view model:
Then in your controller you fill the view model:
And finally your strongly typed view could use a display template. Obviously the view will be strongly typed to IEnumerable
And your display template (~/Views/Home/DisplayTemplates/CategoryViewModel.cshtml):
As you can see with strongly typed views and display templates you don't even need to write loops in your views.
As an alternative to display templates you could use
Html.Action
orHtml.RenderAction
helers. Phil Haack wrote a nice blog post about them. I suspect that you were forced to use the ugly untyped ViewBag because this code is situated in your master template and you don't have a view model. No problem, withHtml.Action
you could have a specific controller that will do this:And then have a corresponding partial view which will take care of rendering the categories (
~/Views/Categories/Index.cshtml
):And finally use the same display template.
Now in your master page you could simply include this child action:
您正在寻找一种称为“部分视图”的东西。
You're looking for something called a 'partial view'.