Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 202
Nate Pet Avatar asked Feb 22 '12 15:02

Nate Pet


People also ask

Which is better MVC or razor?

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.

Do razor pages use MVC?

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.

Should I learn razor pages or MVC?

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 (.


1 Answers

You could implement in view as follows:

@Html.DisplayFor(modelItem => modelItem.FirstName).ToString().Substring(0,5)
like image 53
Weslley Larentes Avatar answered Sep 23 '22 20:09

Weslley Larentes