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
You should put a property on your ViewModel for that instead of trying to get it in the view code. The views only responsibility is to display what is given to it by the model, it shouldn't be creating new data from the model.
You can use a custom extension method as shown below:
Hope this helps...
If you are only wanting to display the first character of
item.FirstName
why not do:You have it the wrong side of the closing bracket.
Might I suggest that the view is not the right place to do this. You should probably have a separate model property,
FirstInitial
, that contains the logic. Your view should simply display this.This worked for me (no helper):
You could implement in view as follows: