渲染部分 ViewData 父/子

发布于 2024-08-11 01:29:10 字数 1406 浏览 2 评论 0原文

我有一系列货架,每个货架都有一系列产品。为了优化重用,我创建了一个局部视图 ProductList.ascx,当我循环浏览货架列表时会调用该视图。在部分视图中,我希望为每种类型的产品添加一个“添加”链接,并且需要货架 ID 才能执行此操作。由于部分视图是产品的集合,并且该货架上可能还没有产品,因此我如何添加指向包含货架 ID 的部分的添加链接?

父视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ShelfDetail>>" %>
<% foreach (ShelfDetail shelf in Model)
               { %>

                    <%= Html.Encode(shelf.Name) %> 
                        <%= Html.ActionLink("Delete Shelf", "Delete", "Shelf", new { Id = shelf.Id }, null)%>
                        <%= Html.ActionLink("Edit Shelf", "Edit", "Shelf", new { Id = shelf.Id }, null) %>

                        <% Html.RenderPartial("SoupList", shelf.Soups); %>
                        <% Html.RenderPartial("PieList", shelf.Pies); %>

                        <p><%= Html.Encode(shelf.Description) %></p>
                    <% } %>
        <% } %>

子汤视图(尝试在显示“此处需要架子 ID”的位置添加路由值):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SoupList>>" %>

Doors: <%= Html.ActionLink("Add Soup", "Add", "Soup", NEED SHELF ID HERE, null)%>
<% foreach (SoupList soup in Model)
{ %>
    <%= Html.Encode(soup.Flavor) %>
<% } %>

I have a collection of Shelves and each shelf has a collection of products. To optimize reuse I have created a partial view ProductList.ascx that is called as I loop through the list of Shelves. In the partial view I want to have an Add link for each type of product, and need the Shelf Id to do so. Since the partial view is a collection of products and may have no products on that shelf yet, how would I include an add link to the partial that includes the Shelf Id?

Parent View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ShelfDetail>>" %>
<% foreach (ShelfDetail shelf in Model)
               { %>

                    <%= Html.Encode(shelf.Name) %> 
                        <%= Html.ActionLink("Delete Shelf", "Delete", "Shelf", new { Id = shelf.Id }, null)%>
                        <%= Html.ActionLink("Edit Shelf", "Edit", "Shelf", new { Id = shelf.Id }, null) %>

                        <% Html.RenderPartial("SoupList", shelf.Soups); %>
                        <% Html.RenderPartial("PieList", shelf.Pies); %>

                        <p><%= Html.Encode(shelf.Description) %></p>
                    <% } %>
        <% } %>

Child Soup View (trying to add the route value where it says "NEED SHELF ID HERE"):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SoupList>>" %>

Doors: <%= Html.ActionLink("Add Soup", "Add", "Soup", NEED SHELF ID HERE, null)%>
<% foreach (SoupList soup in Model)
{ %>
    <%= Html.Encode(soup.Flavor) %>
<% } %>

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

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

发布评论

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

评论(1

夜声 2024-08-18 01:29:10

将 SoupList 的模型类型更改为同时包含架子 ID 和汤列表的类型,例如:

public class SoupPresentation
{
    public Guid ShelfId { get; set; }
    public IEnumerable<SoupList> Soups { get; set; }
}

Change the model type of SoupList to a type which includes both the shelf ID and the list of soups, e.g.:

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