2015-10-15 149 views
-1

我想在下拉列表中创建`@ Html.ActionLink(model.country,“Details”,“Country”)'作为选项..因为以后,不同角色的访问将看到不同的链接...在Html.ActionLink中创建一个动态链接文本和链接()

控制器

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 IM ...

我很新的MVCň.netFramework

回答

2

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

或者尝试这样的:

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

你越来越空,因为你不是PASSI将参数添加到您的操作方法中。

+0

谢谢!第二种方法真的可以工作! 只是为其他reffrence,我认为第一个犯规,因为第一个参数必须是串... – zahrin

+0

进出口新的堆栈......已经起来投票,但似乎那里有它另外一个要求...... 妮甚至不知道如何标记这个职位为解决.... – zahrin

+0

你已经做了 –

0

谢谢!第二种方法真的有用!

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

然而,这个方法我试过,但还是同样的错误抛出......这个参数为空...

其实,我并通过viewbag传递参数。 ..或这是不是所谓的“传递参数”,我不知道...但是,当我将@ Html.actionlink更改为@item,它没有项目列表...

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