简体   繁体   中英

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

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);

This error:

Value cannot be null or empty. Имя параметра: linkText

only says that the linkText parameter is needed (he can't be null or empty ) in the ActionLink method.

for example you could write like this:

 @Html.ActionLink(" ","List", "Profile")

it should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM