0

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:

Community
  • 1
  • 1
  • Could you please clarify your question a bit? `@Html.DisplayNameFor(model => model.LastName)` should display "LastName". What other HtmlHelper are you trying to use? You may just need to use the string "Last Name". – abalter Jun 07 '15 at 02:33
  • Also please tell us how the "column title" is specified. Columns don't have titles, in general. – John Saunders Jun 07 '15 at 02:53
  • in general for just passing the value use model.LastName, the Lambda is used to point at the property the value comes from to find other metadata for display and validation – Yishai Galatzer Jun 07 '15 at 02:56
  • I think by "column title" he means "field name." – abalter Jun 07 '15 at 03:17
  • Maybe you could give a little background about why you are trying to achieve this. For instance, why is it not good enough to just put the field name in the action link: `@Html.ActionLink("Field Name", "Linked View")`. – abalter Jun 07 '15 at 03:19
  • @abalter: ActionLink is the other HtmlHelper that I'm trying to use. Passing in "Last Name" as string works fine, but that's hard-coded. – David Oliver Jun 07 '15 at 05:19
  • to YishaiGalatzer: model.LastName (through loops) returns the value of an instance, great; the Lambda returns the property, supper. But I'm working with the table heading (expecting them to be links), and now I want the string "Last Name" to pass in as a parameter of @Html.ActionLink. I can give ActionLink a string "Last Name" as a parameter, but I don't want to hard-code it. Something like Model.LastName.DisplayNameThatIsGivenAsAttributeInClassDesign() would be really helpful in my case. – David Oliver Jun 07 '15 at 05:56
  • Right, you want something like `@Html.ActionLink(@Html.LabelFor(model=>model.FieldName), "Page"))`. But I tried it, and I don't think you can nest helpers. Would you accept a hybrid Ajax solution? If so, I think I can figure one out. – abalter Jun 07 '15 at 07:19
  • Could you provide just a bit more information about how you want to use it . Like, do you want to be able to use a for loop to create a set of links for each column name? – abalter Jun 07 '15 at 08:02
  • `@Html.ActionLink()` does not accept an expression as a parameter. You will need to write your own html helper extension method. –  Jun 07 '15 at 08:08
  • Thank you abalter, but I think using resources (from Alex Art) is the way to go. That would help me later on the I18N issue as well. – David Oliver Jun 07 '15 at 08:09
  • @Stephen Muecke: `DisplayNameFor` HtmlHelper extension from [link](http://stackoverflow.com/questions/4947854/how-to-get-the-column-titles-from-the-displayname-dataannotation-for-a-strong?rq=1) wouldn't help. I need a string to pass onto `Html.ActionLink()`, not MvcHtmlString. – David Oliver Jun 07 '15 at 08:47
  • No I mean you need to create your own html helper method that accepts an expression! –  Jun 07 '15 at 08:48
  • Well, that clarifies it. But ... `ContosoUniversity` ... I'm still learning the basics. Anyhow, thank you all! Using resources seems to be the solution for me. – David Oliver Jun 07 '15 at 08:56

1 Answers1

0

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>
    
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
  • It's just what I need. Thanks so much! Although I'm sure there's a way for me to manage all of these resources (like in a folder) or something right? Because later on I would want properties on other classes to have ResourceType like this one. How do I make it centralized in like one folder only? – David Oliver Jun 07 '15 at 07:37
  • I'm trying out your solution. And.... How do I add Resource LastNameTitle again? :smiley face: – David Oliver Jun 07 '15 at 07:50
  • Take a look at this link http://www.codeproject.com/Articles/778040/Beginners-Tutorial-on-Globalization-and-Localizati You can get an Idea on how to use resources – Alex Art. Jun 07 '15 at 07:54
  • This is a useful link as well. It contains a lot of code for I18N but it also explains how to work with resources – Alex Art. Jun 07 '15 at 07:56
  • These might also be useful: https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displaycolumnattribute(v=vs.95).aspx https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.datatypeattribute(v=vs.95).aspx – abalter Jun 07 '15 at 08:00
  • Wonderful! I'm a little lazy here. Help me out will you? When (in Student Index View) `@model IEnumerable` became `@model PagedList.IPagedList` , this `@Html.DisplayNameFor(model => model.FirstName) ` causes an error. :( – David Oliver Jun 07 '15 at 08:03
  • Anyway, thank you. The problem has been resolved by using resources. – David Oliver Jun 07 '15 at 08:40
  • Regarding your issue, you cant use `@Html.DisplayNameFor(model => model.FirstName)` since you model now is not a student but a `IEnumerable`. and clearly `IEnumerable` doesn't have FirstName property – Alex Art. Jun 07 '15 at 08:54
  • It's actually from a IEnumerable list of student to a paged list of student. :D – David Oliver Jun 07 '15 at 08:59
  • Well, it actually doesn't matter since `PagedList.IPagedList` doesn't have FirstName property either. – Alex Art. Jun 07 '15 at 09:04