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

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 in the correct data. Now, in my opinion, I:

 @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" does not have an applicable method named "Image", but this name has an extension method. Extension methods cannot be dynamically dispatched. Think about how to use dynamic arguments or invoke an extension method without extension method syntax.

Any help would be greatly appreciated, thanks in advance.

+4
source share
1 answer

Warning: I did not make any MVC materials, so this is guesswork.

I assume ActivePlans is dynamic in one way or another ... so you basically need to make item strongly typed. It could be that simple:

 @foreach (RegisterSubscription item in ViewBag.ActivePlans) 

... which will basically drop every element before RegisterSubscription , as it pulled it from the ViewBag . After this point, I don’t see that you have something dynamic, so the extension method should be fine. I think. May be. When the wind is right.

+13
source

Source: https://habr.com/ru/post/1335066/


All Articles