Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi ,

I wanted to add an special character : in my razor view which is currently binded via lambda expression

@Html.DisplayFor(x => x.StoreProfileAssociation.RegionalManager.Title)


What I have tried:

I tried in this way but throws an exception

@Html.DisplayFor(string.Format("{0}{1}", x => x.StoreProfileAssociation.RegionalManager.Title,":")
Posted
Updated 20-Apr-16 4:24am
Comments
Sergey Alexandrovich Kryukov 20-Apr-16 7:15am    
Are some characters "special"? I never heard of that. Which one? Why may it possibly create such complications?
—SA
sahmed3 20-Apr-16 7:38am    
Hi,
Basically i wanted to show colon(:) after my Title.
Sergey Alexandrovich Kryukov 20-Apr-16 10:20am    
How special! Anyway, just write the colon. :-)
—SA

Create a Folder named DisplayTemplates under Views-> Shared -> DisplayTemplates Folder

  • Views
    • Shared
      • DisplayTemplates




Create a view as MyFormat.cshtml inside DisplayTemplates Folder
write the following code in the cshtml file for custom formatting
C#
@model string 
@string.Format("{0}:",Model)



and then your razor as

C#
@Html.DisplayFor(model => model.PropertyName,"MyFormat" )


referred from Display For Templates[^]
 
Share this answer
 
Your problem is: first parameter of your call to string.Format is the delegate instance, which makes no sense at all. You need some value instead, not the method to find value; and then the formatted string will include anything returned by System.Object.ToString() called on that object.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900