0

I need to get the first initial of the first name

I have the following:

     @Html.DisplayFor(modelItem => item.FirstName.Substring(1,1)) 

I get the following error though when the program tries to run it:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

4
  • possible duplicate of MVC Razor need to get Substring
    – tvanfosson
    Commented Feb 22, 2012 at 16:17
  • You just asked this exact question 20 minutes ago and got the same answer.
    – tvanfosson
    Commented Feb 22, 2012 at 16:18
  • 2
    @tvanfosson, no idea why the OP accepted this answer in his previous question as the answer is completely wrong and won't work. Commented Feb 22, 2012 at 16:21
  • What would prevent you from using @item.FirstName.Substring(1,1)?
    – Nick Bork
    Commented Feb 22, 2012 at 16:51

3 Answers 3

1

Simply add a property to your view model:

public string FirstName { get; set; }

public string FirstLetterOfName
{
    get
    {
        // TODO: a minimum amount of error checking
        // might be good here for cases when FirstName
        // is null or an empty string
        return this.FirstName.Substring(1, 1);
    }
}

and then:

@Html.DisplayFor(modelItem => item.FirstLetterOfName) 

And if you now tell me that you are not using view models but are directly passing your domain entities to your views (which is very bad design), you might use a custom template:

@Html.DisplayFor(modelItem => item.FirstName, "FirstLetter") 

and then you define a custom display template ~/Views/Shared/DisplayTemplates/FirstLetter.cshtml with the following contents:

@ViewData.TemplateInfo.FormattedModelValue.Substring(1, 1)
2
  • so I can add this all in my view?
    – Nate Pet
    Commented Feb 22, 2012 at 16:13
  • @NatePet, no, first you change your view model class (the one that contains the FirstName property) to add the property and then you use this new property in your view. Commented Feb 22, 2012 at 16:14
0

You can't do this, because the mapping to your abject wont work like this. Like the exception message tells you you can only use it with properties.

You can however write your own HtmlHelper extension to do what you want

public static class HtmlHelpers
{
    public static MvcHtmlString CustomDisplayFor(this HtmlHelper<Person> htmlHelper, Expression<Func<Person, string>> expression)
    {
          var customBuildString = string.Empty;
          //Make your custom implementation here
          return MvcHtmlString.Create(customBuildString);
    }
}
0

Before substring, check the length of the string, else you will get index out of range exception;

@MvcHtmlString.Create(item.title.Length > 20 ? @item.title.ToString().Substring(0, 20):@item.title.ToString())
1
  • @if (!string.IsNullOrEmpty(item.title)){ //your code }
    – Dorath
    Commented Jul 15, 2016 at 12:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.