对每个循环使用部分视图时出现问题
我在这里有点困惑,我尝试在每个循环中使用部分视图,就像这样
<% foreach (var item in (IEnumerable<MVCLD.Models.Article>)ViewData["LatestWebsites"]){%>
<% Html.RenderPartial("articlelisttemaple", item); %>
<% } %>
我的部分视图看起来像这样
<div class="listingholders">
<h4><%=Html.ActionLink(item.ArticleTitle, "details", "article", new { UrlID = item.UrlID, ArticleName = item.ArticleTitle.ToString().niceurl() }, null)%> </h4>
<p><%= Html.Encode(item.ArticleSnippet) %></p>
<div class="clearer"> </div>
</div>
但是当我运行该项目时,我被告知部分视图不明白什么是项目?
CS0103: The name 'item' does not exist in the current context
我不明白为什么当我将项目传递到部分视图时它会这样做?
I'm a little confused here, I am trying use a partial view in a for each loop like so
<% foreach (var item in (IEnumerable<MVCLD.Models.Article>)ViewData["LatestWebsites"]){%>
<% Html.RenderPartial("articlelisttemaple", item); %>
<% } %>
And my partial view looks like this
<div class="listingholders">
<h4><%=Html.ActionLink(item.ArticleTitle, "details", "article", new { UrlID = item.UrlID, ArticleName = item.ArticleTitle.ToString().niceurl() }, null)%> </h4>
<p><%= Html.Encode(item.ArticleSnippet) %></p>
<div class="clearer"> </div>
</div>
But when I run the project I get told the partial view doesn't understand what item is??
CS0103: The name 'item' does not exist in the current context
I can't see why it would be doing this as I'm passing item into the partial view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将部分视图更改为:
部分视图必须继承自
System.Web.Mvc.ViewUserControl
。部分视图的第一行应如下所示:
Change partial view to:
Partial view must inherit from
System.Web.Mvc.ViewUserControl<MVCLD.Models.Article>
.This is how first line of partial view should look like:
如果您已声明使用 MVCLD.Models.Article 作为部分的模型,则部分中的项目应该是模型!
Item in the partial should be model if you have declared you're using MVCLD.Models.Article as a model for the partial !
您的部分控件“articlelisttemaple.ascx”应在控件顶部具有以下内容。
这表明该视图具有类型为“MVCLD.Models.Article”的模型。
您应该使用“Model”,而不是部分视图中的“item”。
Your partial control "articlelisttemaple.ascx" should have the following at the top of the control.
This indicates that the view has a model of the type : "MVCLD.Models.Article".
Instead of "item" in the partial view, you should use "Model".