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
From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.
Razor pages do not have any type of structure like MVC. But all Razor pages inbuilt under the Pages folder with a very simple structure.
Razor Pages represents a simpler way to generate HTML on the server compared to MVC. It is recommended for all new web applications that rely on server-side HTML generation going forward. MVC is still available for existing apps. It is also probably easier to migrate older MVC 5 (.
You could implement in view as follows:
@Html.DisplayFor(modelItem => modelItem.FirstName).ToString().Substring(0,5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With