0

I have a main page which uses a ViewModel I have created:

public class HomeViewModel
{
    public List<Category> Categories { get; set; }
    public List<Image> Images { get; set; }

    public HomeViewModel(List<Image> images, List<Category> categories)
    {
        Images = images;
        Categories = categories;
    }
}

On my main page I am outputing the Categories but then passing the Images through to a PartialView.

<div id="gallery">
    <% Html.RenderPartial("gallery", Model.Images); %>
</div>

When I run my work I am getting this error: error CS0308: The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments

This is my partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Gallery.Models.Image>>" %>
    <%@ Import Namespace="Gallery.Helpers" %>

    <ul>
        <% foreach (var item in Model)
        { %>
            <li>             
                <%= Html.ImageLink(item.imageID.ToString(), Url.Content("~/Uploads/" + item.imageLocation).ToString(), item.imageDescription, Url.Content("~/Uploads/" + item.imageThumb).ToString(), item.imageDescription, null)%>
            </li>
        <% } %>
    </ul>

I was using .NET 4 and this worked fine but I understand they have altered Generics since 3.5 which made it possible. However my web server company hasn't upgraded their servers meaning I have to deploy using 3.5. I have got the Partial to work if I cast the Partial as the same object as the Main page but then I am passing through more ViewData than I need to. The reason I am using Partials is to take advantage of AJAX in the .NET framework. E.g. the Categories are AJAX links goto a method that method returns an updated Partial with the category images, I should only need to pass Images into the Parial.

Any help on solving this would be appreciated.

Thanks,

Jon

1 Answer 1

4

I suspect that you are not importing the System.Collections.Generic namespace for your ASPX/ASCX files so the compiler things you're using the non-generic IEnumerable.

Go to your web.config file and ensure that all the necessary namespaces are being imported.

Alternatively, use the full type name in your "Inherits" attribute:

Inherits="System.Web.Mvc.ViewUserControl<System.Collections.Generic.IEnumerable<Gallery.Models.Image>>"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. This has resolved the issue, I'm still quite new to .NET mainly used Java before :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.