Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The issue you are going to have is if the name is blank, there is nothing to click on for the user in the UI. Example: <code>&lt;a href="someUrl"&gt;&lt;/a&gt;</code> gives no clickable object.</p> <p>What may be better is if you could provide a default "No Name" string to those rows that have an empty string.</p> <pre><code>@foreach (var row in Model) { &lt;li&gt;@Html.ActionLink(string.isNullOrEmpty(row.Name) ? "No Name" : row.Name, "Action", "Controller", new { id = row.Id })&lt;/li&gt; } </code></pre> <p><strong>EDIT</strong></p> <p>If you do not want to put a condition in every @Html.ActionLink, you can create your own @Html.Helper</p> <pre><code> public static IHtmlString ActionLinkCheckNull(this HtmlHelper htmlHelper, string linkText, string action, string controller, object routeValues, object htmlAttributes) { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); var anchor = new TagBuilder("a") { InnerHtml = string.IsNullOrEmpty(linkText) ? "No Name", linktext }; anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues); anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return MvcHtmlString.Create(anchor.ToString()); } </code></pre> <p>Then in your Views folder web.config, add a reference to the namespace that you place this extension so you don't have to have a using statement at the top of every view. Example:</p> <pre><code>&lt;add namespace="Core.Extensions"/&gt; </code></pre> <p>Then in your views, use</p> <pre><code> @Html.ActionLinkCheckNull(row.Name, "Action", "Controller", new { id = row.Id }) </code></pre>
 

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