Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To further concrete what Tommy is saying, I wanted to include the ultimately called method that's being protected:</p> <pre><code>private static string GenerateLinkInternal(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary&lt;string, object&gt; htmlAttributes, bool includeImplicitMvcValues) { string value = UrlHelper.GenerateUrl(routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, routeCollection, requestContext, includeImplicitMvcValues); TagBuilder tagBuilder = new TagBuilder("a") { InnerHtml = (!string.IsNullOrEmpty(linkText)) ? HttpUtility.HtmlEncode(linkText) : string.Empty }; tagBuilder.MergeAttributes&lt;string, object&gt;(htmlAttributes); tagBuilder.MergeAttribute("href", value); return tagBuilder.ToString(TagRenderMode.Normal); } </code></pre> <p>so, since you're building a link, the outer method is throwing an exception because <strong>it wouldn't have any text to show on the screen.</strong> That code is shown below:</p> <pre><code>public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary&lt;string, object&gt; htmlAttributes) { if (string.IsNullOrEmpty(linkText)) { throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText"); } return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null, actionName, controllerName, routeValues, htmlAttributes)); } </code></pre> <p>technically speaking that code doesn't <strong>have to be</strong> protected but it is as a <em>feature</em>.</p> <hr> <p>Here is a workaround for you, but you'll need to build your own <code>ActionLink</code> extension method like this and place it in a code file somewhere in the project:</p> <pre><code>using System; using System.Collections.Generic; using System.Web.Mvc.Resources; using System.Web.Routing; namespace System.Web.Mvc.Html { public static class MyLinkExtensions { public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, int identifier) { return htmlHelper.ActionLink(string.IsNullOrEmpty(linkText) ? "some default text" : linkText, actionName, controllerName, new { id = identifier }); } } } </code></pre> <p>and so now your markup looks like this:</p> <pre><code>@foreach (var row in Model) { &lt;li&gt;@Html.ActionLink(row.Name, "Action", "Controller", row.Id)&lt;/li&gt; } </code></pre> <p>and you have encapsulated the turnary to determine the state of the <code>linkText</code> and yet still render your links in a single line.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload