【问题标题】:create a dynamic link text and link in Html.ActionLink()在 Html.ActionLink() 中创建动态链接文本和链接
【发布时间】:2016-01-13 15:55:16
【问题描述】:

我想在下拉列表中创建 `@Html.ActionLink(model.country, "Details", "Country")' 作为选项..因为稍后,不同的角色访问会看到不同的链接...

控制器

    public ActionResult Index()
    {
        CountryList = db.CountryListTable.Select(x => x.countryName).ToList();
        ViewBag.ctr = CountryList;
        return View();
    }

查看

    @foreach (var item in @ViewBag.ctr)
    {
            <span>@item</span>
            <span>
            @* @Html.ActionLink((string)@item, "Details", "Country"); *@ @* this part throw error due to null *@
            </span>
            <br />
    }

@item 不为空...我不知道为什么用@html.ActionLink 封装时会抛出空值...

仅供参考,我正在使用 .netFramework 4.0 和 MVC4...

我是全新的 MVC n .netFramework

【问题讨论】:

    标签: c# asp.net-mvc razor linked-list ienumerable


    【解决方案1】:

    应该是这样的: Html.ActionLink("Details", "Country", new { country = @item })

    或者这样试试:

    &lt;a href="@Url.Action("Details", "Country", new { country = @item })"&gt;@item&lt;/a&gt;

    你得到了 null,因为你没有将参数传递给你的 action 方法。

    【讨论】:

    • 谢谢!第二种方法确实有效!只是为了其他参考,我认为第一个不是因为第一个参数需要是字符串...
    【解决方案2】:

    谢谢!第二种方法确实有效!

    仅供参考,我认为第一个不是因为第一个参数需要是字符串... 所以应该是:Html.ActionLink((string)item, "Details", "Country", new { country = @item })

    但是,我尝试了这种方法,但仍然抛出相同的错误...此参数为空...

    实际上,我确实通过 viewbag 传递了参数...或者这不是所谓的“传递参数”,我不知道...但是当我将此 @Html.actionlink 更改为 @item 时,它确实列出项目...

    感谢 Arjit Mukherjee...您确实为我节省了很多时间...

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多