渲染部分 ViewData 父/子
我有一系列货架,每个货架都有一系列产品。为了优化重用,我创建了一个局部视图 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 SoupList 的模型类型更改为同时包含架子 ID 和汤列表的类型,例如:
Change the model type of SoupList to a type which includes both the shelf ID and the list of soups, e.g.: