【问题标题】:Database access in asp.netasp.net 中的数据库访问
【发布时间】:2013-05-06 00:32:49
【问题描述】:

我对@9​​87654322@ 视图引擎不是很熟悉。我试过这段代码。

@for(var item in ViewBag.list)
{
    @foreach (var itemvote in ViewBag.listVote)
    {
        <h1>@Html.ActionLink(@item.Title, "Details", "Report", new { id = item.Id},null)</h1>
    }
}

它显示以下错误:

编译器错误消息:CS1973:“System.Web.Mvc.HtmlHelper”没有名为“ActionLink”的适用方法,但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。

我的控制器类是ReportController,方法是Details,它将被提交到。

public ActionResult Details(int id = 0)
        {
            Report report = Context.Reports.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            ViewBag.report = report;

            return View();
        }

我用谷歌搜索了一些链接,例如HTML.ActionLink method

但我仍然无法纠正它。

【问题讨论】:

    标签: asp.net-mvc razor html-helper


    【解决方案1】:

    您的号召性用语链接至少存在 2 个问题。

    首先,第一个参数不需要'@',因为您已经在@Html.ActionLink 的行首使用了一个。

    其次,第一个参数应该是itemvote.Title 而不是 item.Title,因为您没有名为 item 的变量。

    这可能是您的问题的一部分,因为编译器可能没有意识到 @item.Title 应该是一个字符串,并且存在 ActionLink(string, string, string, Object, Object) 的有效签名

    【讨论】:

    • 我解决了这个问题,

      @Html.ActionLink((string)@item.Title, "Details", new { id = item.Id})

      这行是我的回答..
    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2013-01-25
    相关资源
    最近更新 更多