In ASP MVC C# I putted a List(Cars) in the ViewBag.cars, now I want to make an actionlink with the title of each car, like this:
@if (ViewBag.cars != null)
{
foreach (var car in ViewBag.cars)
{
<h4>@Html.ActionLink(@car.title, "Detail", "Cars", new { id = @car.id }, new { @class = "more markered" })</h4>
}
}
The error I get when I use @car.title or just car.title as value, I get this error:
CS1973: 'System.Web.Mvc.HtmlHelper<AutoProject.Models.CarDetails>' has no applicable method named 'ActionLink' 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.
What should I fill in as first parameter of the Actionlink?
It's because car
is dynamic, so it doesn't know what the appropriate extension method could be. If you cast title
to string
, and id
to object
, it'll be fine:
<h4>@Html.ActionLink((string) car.title, "Detail", "Cars", new { id = (object) car.id }, new { @class = "more markered" })</h4>
Your other option is to make a strongly typed ViewModel.