加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – ViewBag的Html.ActionLink值

发布时间:2020-12-16 07:37:20 所属栏目:asp.Net 来源:网络整理
导读:在ASP MVC C#中我在ViewBag.cars中放了一个List(汽车),现在我想用每辆汽车的标题创建一个actionlink,如下所示: @if (ViewBag.cars != null){ foreach (var car in ViewBag.cars) { h4@Html.ActionLink(@car.title,"Detail","Cars",new { id = @car.id },new
在ASP MVC C#中我在ViewBag.cars中放了一个List(汽车),现在我想用每辆汽车的标题创建一个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的第一个参数?

解决方法

这是因为汽车是动态的,所以它不知道适当的扩展方法是什么.如果你将title转换为string,并将id转换为object,那就没关系了:

<h4>@Html.ActionLink((string) car.title,new { id = (object) car.id },new { @class = "more markered" })</h4>

您的另一个选择是创建一个强类型的ViewModel.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读