views:

169

answers:

3

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">&nbsp;</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?

+1  A: 

Item in the partial should be model if you have declared you're using MVCLD.Models.Article as a model for the partial !

Morph
If I change the partial to<h4><%=Html.ActionLink(Model.ArticleTitle, "details", "article", new { UrlID = Model.UrlID, ArticleName = Model.ArticleTitle.ToString().niceurl() }, null)%> </h4>I get an error saying CS1061: 'object' does not contain a definition for 'ArticleTitle' and no extension method 'ArticleTitle' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
leen3o
+4  A: 

Change partial view to:

<div class="listingholders">
     <h4><%=Html.ActionLink(Model.ArticleTitle, "details", "article", new { UrlID = Model.UrlID, ArticleName = Model.ArticleTitle.ToString().niceurl() }, null)%> </h4>
     <p><%= Html.Encode(Model.ArticleSnippet) %></p>
     <div class="clearer">&nbsp;</div>
</div>

Partial view must inherit from System.Web.Mvc.ViewUserControl<MVCLD.Models.Article>.

This is how first line of partial view should look like:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MVCLD.Models.Article>" %>
LukLed
Thank you, its obvious now I see it - hopefully I'll get this MVC stuff soon :)
leen3o
+1  A: 

Your partial control "articlelisttemaple.ascx" should have the following at the top of the control.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MVCLD.Models.Article>" %>

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".

Jan