在ASP.MVC C#中,我在ViewBag.cars中添加了一个列表(Cars),现在我想创建一个带有每个汽车标题的actionlink,如下所示:
@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>
}
}当我使用@car.title或car.title作为值时,会得到以下错误:
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.作为Actionlink的第一个参数,我应该填写什么?
发布于 2014-01-09 16:47:40
这是因为car是动态的,所以它不知道合适的扩展方法是什么。如果将title转换为string,将id转换为object,则可以:
<h4>@Html.ActionLink((string) car.title, "Detail", "Cars", new { id = (object) car.id }, new { @class = "more markered" })</h4>另一个选项是创建一个强类型的ViewModel。
发布于 2014-01-09 17:17:19
尝试将您的car.title分配给变量,然后使用该变量
@if (ViewBag.cars != null)
{
foreach (var car in ViewBag.cars)
{
string title = car.title.ToString();
<h4>@Html.ActionLink(title, "Detail", "Cars", new { id = @car.id }, new { @class = "more markered" })</h4>
}
}发布于 2014-01-09 16:44:30
试一试:
foreach (Car car in ViewBag.cars)
{
<h4>@Html.ActionLink(car.title, "Detail", "Cars", new { id = car.id }, new { @class = "more markered" })</h4>
}ps我也会使你的财产大写而不是更低。
https://stackoverflow.com/questions/21026130
复制相似问题