Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Html.ActionLink()中创建动态链接文本和链接_C#_Asp.net Mvc_Razor_Linked List_Ienumerable - Fatal编程技术网

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

C# 在Html.ActionLink()中创建动态链接文本和链接,c#,asp.net-mvc,razor,linked-list,ienumerable,C#,Asp.net Mvc,Razor,Linked List,Ienumerable,我想在下拉列表中创建“@Html.ActionLink(model.country,“Details”,“country”)”作为选项。因为稍后,使用不同的角色访问将看到不同的链接 控制器 public ActionResult Index() { CountryList = db.CountryListTable.Select(x => x.countryName).ToList(); ViewBag.ctr = CountryList;

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

控制器

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

@foreach(@ViewBag.ctr中的变量项)
{
@项目
@*@Html.ActionLink((字符串)@item,“Details”,“Country”);*@*此部分由于null而引发错误*@

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

仅供参考,我正在使用.netframework4.0和MVC4

我是全新的MVC n.netFramework

它应该是这样的:
Html.ActionLink(“详细信息”,“国家”,新{Country=@item})

或者像这样尝试:

由于没有将参数传递给操作方法,因此将得到null。

应该是这样的:
Html.ActionLink(“详细信息”,“国家”,新{Country=@item})

或者像这样尝试:


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

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

作为其他参考,我认为第一个不是,因为第一个参数需要是字符串。。。 所以应该是:Html.ActionLink((字符串)项,“详细信息”,“国家”,新的{Country=@item})

但是,我尝试了这个方法,但是仍然抛出了相同的错误…这个参数为null

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


谢谢Arjit Mukherjee…你为我节省了很多时间…

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

作为其他参考,我认为第一个不是,因为第一个参数需要是字符串。。。 所以应该是:Html.ActionLink((字符串)项,“详细信息”,“国家”,新的{Country=@item})

但是,我尝试了这个方法,但是仍然抛出了相同的错误…这个参数为null

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


感谢Arjit Mukherjee…你确实为我节省了很多时间…

谢谢!第二种方法确实有效!就为了其他参考,我认为第一个不是,因为第一个参数必须是字符串…谢谢!第二种方法确实有效!对于其他引用,我认为第一个不是,因为第一个参数必须是字符串。。。
    @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 />
    }