Please notice the font tag is deprecated...you shoud not use it. However you can use just a <span> tag where you specify the color as in the font tag.
If you return a simple string into an Mvc View it is automatically Html encoded. so the user will simply see the original html tags instead of a colred string.
To avoid that the string is encoded use the MvcHtmlString type in the controller where you build the string. It is easy just build you string, say "MyString" normally, and then convert it into an MvcHtnlString this way:
YashaJan2011
Member
42 Points
58 Posts
string in MVC
Feb 04, 2012 07:00 AM|LINK
Hi,
I want to use different colors in a string which goes inside a dataTable. i'm setting everything in a controller and passing it to View.
If i set aRow as my datarow then I want to use:
aRow[i] = String.Format("<font color='#CCCCFF'>{0}</font> - ({1})", strOne, strTwo);
but somehow this is not working in MVC
Thank you in advance
francesco ab...
All-Star
20944 Points
3286 Posts
Re: string in MVC
Feb 04, 2012 10:42 AM|LINK
Please notice the font tag is deprecated...you shoud not use it. However you can use just a <span> tag where you specify the color as in the font tag.
If you return a simple string into an Mvc View it is automatically Html encoded. so the user will simply see the original html tags instead of a colred string.
To avoid that the string is encoded use the MvcHtmlString type in the controller where you build the string. It is easy just build you string, say "MyString" normally, and then convert it into an MvcHtnlString this way:
MvcHtmlString.Create(MyString)
Mvc Controls Toolkit | Data Moving Plug-in Videos
dLcreations
Member
593 Points
244 Posts
Re: string in MVC
Feb 04, 2012 11:42 AM|LINK
You can Go Like This
@Html.Grid(Model.ExampleList) .RowAttributes(row => new Hash(@class => row.Item.IsFoo ? "redclass" : "normalclass")) .Columns(c => { ... }) andcolumn.For(p => p.a).Atributes(p=> { if (p.Item.Status == "Active") { return new Dictionary<string, object>{{ "style", "color:green" }}; } else if (p.Item.Status == "In-active") { return new Dictionary<string, object>{{ "style", "color:red" }}; } return new Dictionary<string, object>(); });