Search code examples
c#asp.net-mvcrazor

MVC Razor need to get Substring


I have the following inside of my view

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

I need to get the first initial of the First Name.

I tried

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

but it does not seem to work. I get the following error: .. 'System.Web.Mvc.MvcHtmlString' does not contain a definition for 'Substring' and no extension


Solution

  • If you are only wanting to display the first character of item.FirstName why not do:

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

    You have it the wrong side of the closing bracket.