我应该把这段代码放在哪里?

发布于 2024-10-07 22:35:07 字数 1510 浏览 2 评论 0原文

下面是编写在控制器中的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2024-10-14 22:35:07

与往常一样,您首先创建视图模型:

public class CategoryViewModel
{
    public string CategoryName { get; set; }
}

然后在控制器中填充视图模型:

public ActionResult Index()
{
    var categories = categoryRepo.FindAllCategories();
    var model = MapModelToViewModel(categories);
    return View(model);
}

最后,强类型视图可以使用显示模板。显然,视图将被强类型化为 IEnumerable

<ul>
    @Html.EditorForModel()

    <!-- ActionLink goes: Text, ActionName, Controller -->
    <li>@Html.ActionLink("Libros", "Index", "Home")</li>
    ...
</ul>            

并且您的显示模板(~/Views/Home/DisplayTemplates/CategoryViewModel.cshtml):

@model YourApp.Models.CategoryViewModel
<li>@Model.CategoryName</li>

正如您所见,使用强类型视图和显示模板,您甚至不需要在视图中编写循环。

作为显示模板的替代方案,您可以使用 Html.ActionHtml.RenderAction 工具。 Phil Haack 写了一篇关于它们的不错的博客文章。我怀疑您被迫使用丑陋的无类型 ViewBag,因为此代码位于您的主模板中,并且您没有视图模型。没问题,使用 Html.Action 您可以有一个特定的控制器来执行此操作:

public class CategoriesController: Controller
{
    public ActionResult Index()
    {
        var categories = categoryRepo.FindAllCategories();
        var model = MapModelToViewModel(categories);
        return View(model);
    }
}

然后有一个相应的部分视图,它将负责呈现类别(~/Views/Categories/ Index.cshtml):

@model IEnumerable<YourApp.Models.CategoryViewModel>
@Html.DisplayForModel()

最后使用相同的显示模板。

现在,在您的母版页中,您可以简单地包含此子操作:

<ul>
    @Html.Action("index", "categories")

    <!-- ActionLink goes: Text, ActionName, Controller -->
    <li>@Html.ActionLink("Libros", "Index", "Home")</li>
    ...
</ul>            

As always you start by creating a view model:

public class CategoryViewModel
{
    public string CategoryName { get; set; }
}

Then in your controller you fill the view model:

public ActionResult Index()
{
    var categories = categoryRepo.FindAllCategories();
    var model = MapModelToViewModel(categories);
    return View(model);
}

And finally your strongly typed view could use a display template. Obviously the view will be strongly typed to IEnumerable

<ul>
    @Html.EditorForModel()

    <!-- ActionLink goes: Text, ActionName, Controller -->
    <li>@Html.ActionLink("Libros", "Index", "Home")</li>
    ...
</ul>            

And your display template (~/Views/Home/DisplayTemplates/CategoryViewModel.cshtml):

@model YourApp.Models.CategoryViewModel
<li>@Model.CategoryName</li>

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 or Html.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, with Html.Action you could have a specific controller that will do this:

public class CategoriesController: Controller
{
    public ActionResult Index()
    {
        var categories = categoryRepo.FindAllCategories();
        var model = MapModelToViewModel(categories);
        return View(model);
    }
}

And then have a corresponding partial view which will take care of rendering the categories (~/Views/Categories/Index.cshtml):

@model IEnumerable<YourApp.Models.CategoryViewModel>
@Html.DisplayForModel()

And finally use the same display template.

Now in your master page you could simply include this child action:

<ul>
    @Html.Action("index", "categories")

    <!-- ActionLink goes: Text, ActionName, Controller -->
    <li>@Html.ActionLink("Libros", "Index", "Home")</li>
    ...
</ul>            
眼前雾蒙蒙 2024-10-14 22:35:07

您正在寻找一种称为“部分视图”的东西。

You're looking for something called a 'partial view'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文