Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MVC3 ViewBag, how to use lists property in view within foreach statement?

public class RegisterSubscription
{
        public string ID { get; set; }
        public string Description { get; set; }
        public int SubscrSelected { get; set; }
        public string Image { get; set; }
        public string InfoUrl { get; set; }
}

private List<RegisterSubscription> activePlans = new List<RegisterSubscription>();

In my controller I have ViewBag.ActivePlans = activePlans; and the ViewBag fills with the correct data. Now in my view I have:

@foreach (var item in ViewBag.ActivePlans)
{
    <li class="popupmenu" id="service_@(item.ID)" >
    <div>
    @Html.Image(item.Image, new { style="border-style:none; text-align:left; vertical-align:middle; width:64px; height:64px" })
    </div>
    </li>
}

but I get the following error: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Image' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

Any help will be greatly appreciated, thank you in advance.

like image 508
zynaps Avatar asked Dec 16 '22 18:12

zynaps


1 Answers

Warning: I haven't done any MVC stuff, so this is guesswork.

My guess is that ActivePlans is dynamic in some form... so you basically need to make item strongly typed. It may be just as simple as this:

@foreach (RegisterSubscription item in ViewBag.ActivePlans)

... which would basically cast each item to RegisterSubscription as it got it out of the ViewBag. After that point, I can't see that you've got anything dynamic, so the extension method should be okay. I think. Maybe. When the wind is right.

like image 187
Jon Skeet Avatar answered Dec 19 '22 08:12

Jon Skeet