Search code examples
asp.net-mvcrazorparameterstitleactionlink

Get Column title in View


MVC5 did a great job on automatically creating the View page (Razor). It currently displays my list of Students for example with table heading like this:

<th>
    @*@Html.ActionLink("", "Index", new { sortOrder = ViewBag.NameSortParm })*@
    @Html.DisplayNameFor(model => model.LastName)
</th>

Now. DisplayNameFor take a Lambda Expression as its parameter. However, I want the Column title, which is "Last Name", to pass into another HtmlHelper's parameter - ActionLink's.
Following articles seem to be similar but I read it, unfortunately it didn't solve my problem:


Solution

  • Why don't you use Resources for this:

    1. Add Resource LastNameTitle
    2. Your model will look like:

      public class Model
      {
      
          [Display(Name = "LastNameTitle ", ResourceType = typeof(Resources.Resources))]
          public string LastName{ get; set; }
      }
      
    3. Your View:

      <th>
          @Html.ActionLink(Resources.Resources.LastNameTitle , "Index", new { sortOrder = ViewBag.NameSortParm })
          @Html.DisplayNameFor(model => model.LastName)
      </th>