[英]Value cannot be null or empty. Parameter name linkText
I have a project that similar on site in "asp.net MVC for professional" book, so i have a problem with navigation bar, problem in view.我在“asp.net MVC for Professional”一书中有一个与现场类似的项目,所以我的导航栏有问题,视图有问题。
the text of error is: Value cannot be null or empty.错误的文本是:值不能为空或空。 Имя параметра: linkText
出处:linkText
@foreach (var link in Model)
{
@Html.RouteLink(link, new
{
controller = "Profile"
,
I understand that problem in link, but I have no idea how to fix it.我了解链接中的问题,但我不知道如何解决。
below code of controllers and view.下面的控制器和视图代码。 Menu.cshtml
菜单.cshtml
@model IEnumerable<string>
@Html.ActionLink("List", "Profile")
@foreach (var link in Model)
{
@Html.RouteLink(link, new
{
controller = "Profile",
action = "List",
category = link,
page = 1
})
}
NavController导航控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HoboAnimal.Domain.Abstract;
namespace HoboAnimal.WebUI.Controllers
{
public class NavController : Controller
{
private IProfileRepository repository;
public NavController(IProfileRepository repo)
{
repository= repo;
}
public PartialViewResult Menu(){
IEnumerable<string> categories = repository.Profiles.
Select(x => x.Category).
Distinct().
OrderBy(x => x);
return PartialView(categories);
}
}
}
Layout布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Content/Site.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="header">
</div>
<div id="categories">
@{Html.Action("Menu","Nav");}
</div>
<div id="content">
@RenderBody()
</div>
</body>
</html>
Thank you谢谢
Since linkText is first argument of ActionLink and RouteLink, this mean that 1 or more of yours "link" in Model is empty string.由于 linkText 是 ActionLink 和 RouteLink 的第一个参数,这意味着模型中的 1 个或多个“链接”是空字符串。 Check it before create link:
在创建链接之前检查它:
@foreach (var link in Model)
{
if(!String.IsNullOrEmpty(link.toString())
{
@Html.RouteLink(link, new
{
controller = "Profile",
action = "List",
category = link,
page = 1
})
}
}
or remove empty rows from selection:或从选择中删除空行:
IEnumerable<string> categories = repository.Profiles.
Select(x => x.Category).
Distinct().
Where(x => !String.IsNullOrEmpty(x)).
OrderBy(x => x);
This error:这个错误:
Value cannot be null or empty.
值不能为 null 或为空。 Имя параметра: linkText
出处:linkText
only says that the linkText parameter is needed (he can't be null or empty ) in the ActionLink method.只说在ActionLink方法中需要linkText参数(他不能为null 或 empty )。
for example you could write like this:例如你可以这样写:
@Html.ActionLink(" ","List", "Profile")
it should help.它应该有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.