在 Html.ActionLink() 中创建动态 link 文本和 link
create a dynamic link text and link in Html.ActionLink()
我想在下拉列表中创建“@Html.ActionLink(model.country, "Details", "Country")”作为选项..因为稍后,具有不同的角色访问将看到不同的 link...
控制器
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 和 .netFramework 的新手
应该是这样的:
Html.ActionLink("Details", "Country", new { country = @item })
或者这样尝试:
<a href="@Url.Action("Details", "Country", new { country = @item })">@item</a>
你得到的是空值,因为你没有将参数传递给你的操作方法。
谢谢!第二种方法确实有效!
仅供参考,我认为第一个不是因为第一个参数需要是字符串...
所以它应该是:Html.ActionLink((string)item, "Details", "Country", new { country = @item })
然而,我尝试了这个方法,但仍然抛出相同的错误...此参数为空...
实际上,我确实通过 viewbag 传递了参数...或者这不是所谓的 "passing parameters",我不知道...但是当我将此 @Html.actionlink 更改为 @项目,它确实列出了项目...
感谢 Arjit Mukherjee...您确实为我节省了很多时间...
我想在下拉列表中创建“@Html.ActionLink(model.country, "Details", "Country")”作为选项..因为稍后,具有不同的角色访问将看到不同的 link...
控制器
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 和 .netFramework 的新手
应该是这样的:
Html.ActionLink("Details", "Country", new { country = @item })
或者这样尝试:
<a href="@Url.Action("Details", "Country", new { country = @item })">@item</a>
你得到的是空值,因为你没有将参数传递给你的操作方法。
谢谢!第二种方法确实有效!
仅供参考,我认为第一个不是因为第一个参数需要是字符串... 所以它应该是:Html.ActionLink((string)item, "Details", "Country", new { country = @item })
然而,我尝试了这个方法,但仍然抛出相同的错误...此参数为空...
实际上,我确实通过 viewbag 传递了参数...或者这不是所谓的 "passing parameters",我不知道...但是当我将此 @Html.actionlink 更改为 @项目,它确实列出了项目...
感谢 Arjit Mukherjee...您确实为我节省了很多时间...