Vitalij Mescheryakov Vitalij Mescheryakov - 3 months ago 33
ASP.NET (C#) Question

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.

the text of error is:
Value cannot be null or empty.
Имя параметра: 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

@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

Answer

Since linkText is first argument of ActionLink and RouteLink, this mean that 1 or more of yours "link" in Model is empty string. 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);