在 Html.ActionLink() 中创建动态链接文本和链接

0 c# asp.net-mvc ienumerable linked-list razor

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

控制器

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

看法

    @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 />
    }
Run Code Online (Sandbox Code Playgroud)

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

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

我对 MVC n .netFramework 很陌生

Ari*_*jee 5

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

或者尝试这样:

<a href="@Url.Action("Details", "Country", new { country = @item })">@item</a>

您将得到 null,因为您没有将参数传递给操作方法。