我在MVC 3视图中有以下代码
<% foreach (var stockItem in Model.Shop.Stock) { %>
<div class="shopItem">
<div class="shopItemLeft">
<% Html.RenderPartial("ItemImageDisplay", stockItem.Item); %>
</div>
<div class="shopItemRight">
<strong><%= stockItem.Item.ToString() %></strong><br />
<%= stockItem.Item.CalculatePrice() %><br />
<%= stockItem.Quantity %> in stock<br />
<%= Html.ActionLink("Buy", "BuyShopItem", new { shopId = Model.Shop.Id, itemId = stockItem.Item.Id }, null) %>
</div>
</div>
<% } %>
当渲染它时,“ItemImageDisplay”局部视图不在我放入的div标签内,而是渲染,好像我放了:
<% foreach (var stockItem in Model.Shop.Stock) { %>
<% Html.RenderPartial("ItemImageDisplay", stockItem.Item); %>
<% } %>
<% foreach (var stockItem in Model.Shop.Stock) { %>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong><%= stockItem.Item.ToString() %></strong><br />
<%= stockItem.Item.CalculatePrice() %><br />
<%= stockItem.Quantity %> in stock<br />
<%= Html.ActionLink("Buy", "BuyShopItem", new { shopId = Model.Shop.Id, itemId = stockItem.Item.Id }, null) %>
</div>
</div>
<% } %>
部分视图的html如下:
<%= Html.Image("~/Content/Images/Items/" + Model.Id + ".png", Model.ToString(), new { onmouseover = "tooltip('item:" + Model.Id + "');", onmousemove = "tooltip('item:" + Model.Id + "');", onmouseout = "tooltipRem();" }) %>
请你能帮助我理解为什么会这样渲染以及我可以做些什么来阻止这种情况发生?
编辑:这是此部分的渲染输出:
<img alt="Can of Pineapples" onmousemove="tooltip('item:1152');" onmouseout="tooltipRem();" onmouseover="tooltip('item:1152');" src="/Content/Images/Items/1152.png" title="Can of Pineapples" /><img alt="Kraken Treats" onmousemove="tooltip('item:1104');" onmouseout="tooltipRem();" onmouseover="tooltip('item:1104');" src="/Content/Images/Items/1104.png" title="Kraken Treats" /><img alt="Orange Jelly" onmousemove="tooltip('item:1279');" onmouseout="tooltipRem();" onmouseover="tooltip('item:1279');" src="/Content/Images/Items/1279.png" title="Orange Jelly" /><img alt="Peanuts" onmousemove="tooltip('item:509');" onmouseout="tooltipRem();" onmouseover="tooltip('item:509');" src="/Content/Images/Items/509.png" title="Peanuts" /><img alt="Fourth Year Anaversary Cake" onmousemove="tooltip('item:859');" onmouseout="tooltipRem();" onmouseover="tooltip('item:859');" src="/Content/Images/Items/859.png" title="Fourth Year Anaversary Cake" /><img alt="Taco" onmousemove="tooltip('item:486');" onmouseout="tooltipRem();" onmouseover="tooltip('item:486');" src="/Content/Images/Items/486.png" title="Taco" /><img alt="Can of Blueberries" onmousemove="tooltip('item:1154');" onmouseout="tooltipRem();" onmouseover="tooltip('item:1154');" src="/Content/Images/Items/1154.png" title="Can of Blueberries" /><img alt="Strawberry Jam" onmousemove="tooltip('item:639');" onmouseout="tooltipRem();" onmouseover="tooltip('item:639');" src="/Content/Images/Items/639.png" title="Strawberry Jam" /><img alt="Crisps" onmousemove="tooltip('item:726');" onmouseout="tooltipRem();" onmouseover="tooltip('item:726');" src="/Content/Images/Items/726.png" title="Crisps" /><img alt="Cooked Shrimp" onmousemove="tooltip('item:521');" onmouseout="tooltipRem();" onmouseover="tooltip('item:521');" src="/Content/Images/Items/521.png" title="Cooked Shrimp" /><img alt="Fishy Finger" onmousemove="tooltip('item:629');" onmouseout="tooltipRem();" onmouseover="tooltip('item:629');" src="/Content/Images/Items/629.png" title="Fishy Finger" /><img alt="Can of Peaches" onmousemove="tooltip('item:1151');" onmouseout="tooltipRem();" onmouseover="tooltip('item:1151');" src="/Content/Images/Items/1151.png" title="Can of Peaches" />
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Can of Pineapples</strong><br />
940<br />
10 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=1152">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Kraken Treats</strong><br />
934<br />
7 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=1104">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Orange Jelly</strong><br />
827<br />
9 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=1279">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Peanuts</strong><br />
116<br />
8 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=509">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Fourth Year Anaversary Cake</strong><br />
1794<br />
7 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=859">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Taco</strong><br />
950<br />
7 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=486">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Can of Blueberries</strong><br />
317<br />
8 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=1154">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Strawberry Jam</strong><br />
1032<br />
10 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=639">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Crisps</strong><br />
179<br />
10 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=726">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Cooked Shrimp</strong><br />
663<br />
11 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=521">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Fishy Finger</strong><br />
974<br />
10 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=629">Buy</a>
</div>
</div>
<div class="shopItem">
<div class="shopItemLeft">
</div>
<div class="shopItemRight">
<strong>Can of Peaches</strong><br />
786<br />
7 in stock<br />
<a href="/Market/BuyShopItem?shopId=1&itemId=1151">Buy</a>
</div>
</div>
答案 0 :(得分:2)
我怀疑是因为你使用的是Html.RederPartial
而不是Html.Partial
。后者返回一个字符串,而前者直接写入响应流。我有一种偷偷摸摸的感觉,即MVC正在执行整个foreach循环,然后写入最后的响应,这将解释你的结果。