Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could implement in view as follows:

@Html.DisplayFor(modelItem => modelItem.FirstName).ToString().Substring(0,5)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...