MVC 中的部分视图

发布于 2024-10-03 07:53:49 字数 811 浏览 2 评论 0原文

我是 MVC 的新手,刚刚遇到一个需要帮助的场景。

我将简要概述我的场景,就好像它是我更熟悉的 Web 表单应用程序一样。

场景:

我有一个主页,其中列出了最近 10 篇博客文章,以及一个“存档树”(按时间顺序列出了年/月,并包含指向这些年/月中创建的每个博客条目的链接)。

我创建此“存档树”作为用户控件,并将该用户控件输出到我的主页上。

我还在我的主博客页面的一侧输出此存档树用户控件 - 博客页面仅呈现单个博客文章的详细信息。

我还想在我的 Web 应用程序中的其他几个页面上重复使用此存档树控件。

MVC:

我有一个“PhotoController”控制器,它有一个 ActionResult 方法,负责获取所选照片博客文章 (id) 的详细信息。

我想在我的照片博客页面上包含一个部分视图(存档树,将呈现多个记录),它将循环浏览多个照片记录 (List)。

我可以创建一个视图模型,其中包含“照片”属性(用于渲染单个照片博客记录详细信息)和名为“PhotoArchive”的第二个属性,该属性是一个 List

不过,我对这种方法的担忧是,当我在网站上的其他一些部分上重复使用这个“存档树”部分视图时,我不一定希望通过相同的视图模型(包含“照片”和“照片存档”对象)到父视图。例如,我的主页不需要将“照片”对象传递到呈现主页的视图,但我仍然想在主页上的其余 HTML 中输出“存档树”部分视图。

我期待有一个简单的解决方案来解决这个问题,但我现在还不知道,因为我对 MVC 还很陌生。

I'm brand new to MVC and have just come across a scenario that I need some help with.

I'll just briefly outline my scenario as if it were a Web Forms application that I'm far more familiar with.

SCENARIO:

I have a homepage which lists the most recent 10 blog posts, along with an 'Archive Tree' (listing the years/months in chronological order, with links to each of the blog entries made in those years/months).

I create this 'Archive Tree' as a User Control and output that user control on my homepage.

I also output this Archive Tree user control down the side of my main blog page - the blog page renders the details for just a single blog post.

I also want to re-use this Archive Tree control across a couple of other pages in my web application.

MVC:

I have a 'PhotoController' Controller, which has an ActionResult Method responsible for getting the details of the selected Photo Blog post (id).

I want to include a Partial View (the Archive Tree, that will render multiple records) on my Photo Blog page, that will loop through multiple photo records (List<Photo>).

I could create a View Model that consists of a 'Photo' property (for rendering the single photo blog record details) and a 2nd property called 'PhotoArchive', which is a List<Photo>.

My concern with this approach though is that, when I come to re-use this 'Archive Tree' Partial View on some of the other sections across the site, I won't necessarily be wanting to pass through that same View Model (that contains both 'Photo' and 'PhotoArchive' objects) to the parent view. For example, my homepage will not need to pass through a 'Photo' object to the view that renders the homepage, but I still want to output my 'Archive Tree' Partial View amongst the rest of the HTML on my homepage.

I'm expecting there is a simple solution to this which I just don't know at the moment being so new to MVC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

宣告ˉ结束 2024-10-10 07:53:49

有几种方法可以实现此目的:

您可以使用 Html.RenderAction() http://www.asp.net/mvc/videos/aspnet-mvc-2-render-action

或者您可以使用不同的视图模型

class HomePageViewModel {
  IEnumerable<Blog> PhotoArchive { get; set; }
}
class BlogPageViewModel {
  IEnumerable<Blog> PhotoArchive { get; set; }
  Blog Photo { get; set; }
}

并在调用时传递 PhotoArchive 属性Html.RenderPartial("view", model.PhotoArchive)

或者您可以使用和接口

interface IMyViewPage {
  IEnumerable<Blog> PhotoArchive { get; set; }
}
class MyViewModel : IMyViewPage{
  //...
  Blog PhotoBlog { get; set; }
}

Model.RenderPartial("view", model)

There's a few ways to achieve this:

You could just use Html.RenderAction() http://www.asp.net/mvc/videos/aspnet-mvc-2-render-action

Or you could use different view models

class HomePageViewModel {
  IEnumerable<Blog> PhotoArchive { get; set; }
}
class BlogPageViewModel {
  IEnumerable<Blog> PhotoArchive { get; set; }
  Blog Photo { get; set; }
}

and pass the PhotoArchive property when calling Html.RenderPartial("view", model.PhotoArchive)

Or you could use and interface

interface IMyViewPage {
  IEnumerable<Blog> PhotoArchive { get; set; }
}
class MyViewModel : IMyViewPage{
  //...
  Blog PhotoBlog { get; set; }
}

and Model.RenderPartial("view", model)

混浊又暗下来 2024-10-10 07:53:49

您可以通过创建部分视图的模型 List(或者更好的是 IEnumerable)来解决此问题。当您调用 RenderPartial 时,可以使用 (string, object) 重载以指定要传递给视图以供其呈现的对象:

<% Html.RenderPartial("PartialView.ascx", ViewData.Model.PhotoArchive) %>

You can resolve this problem by making your partial view's model List<Photo> (or even better, IEnumerable<Photo>). When you call RenderPartial, you can use the (string, object) overload to specify the object to pass to the view for it to present:

<% Html.RenderPartial("PartialView.ascx", ViewData.Model.PhotoArchive) %>
寄居者 2024-10-10 07:53:49

定义一个接口,例如 IArchiveTree,它描述树视图模型的结构,然后使所有希望显示为树的模型对象实现该接口。

Define an interface, e.g. IArchiveTree, that describes the structure of the tree view model, then make all the model objects that you wish to display as trees implement that interface.

清引 2024-10-10 07:53:49

在 Index.cshtml 中

     @Html.Partial("_Index");

在 _index.cshtml 中

   <table>
     whatever 
   </table

In the Index.cshtml

     @Html.Partial("_Index");

In the _index.cshtml

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