Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:42+00:00 2026-06-13T12:59:42+00:00

Is it possible to conditionally set a CSS class or encode a string to

  • 0

Is it possible to conditionally set a CSS class or encode a string to html for a certain web grid column? I was able to append span tags to the item using format: in the column, but now I need to add a class to the span tags based on a helper function in razor view or a bool expression from the controller/model. I do not want to do this with JS, C# should suffice.

Here is what I tried in view model:

 @grid.GetHtml(
 headerStyle:"reviewGridHeader",
    columns: grid.Columns(
    ...[other columns]...
    grid.Column(columnName: "FeedBack", format: @<span>@CheckFeedBack(item.FeedBack)</span>)
))

Helper function in view model (splits and compares string):

    static string CheckFeedBack(string item) {
        String[] feedbacks = System.Text.RegularExpressions.Regex.Split(item, "of");

        //if feedback complete
        if((Convert.ToInt32(feedbacks[0]) == (Convert.ToInt32(feedbacks[1]))))
        {
            string newFeedback = @"<span class=""feedBackComplete"">"+ item +"</span>";
            string encodedFeedback = System.Web.HttpUtility.HtmlEncode(newFeedback);

            return encodedFeedback;
        }
        return item;
    }

And here is the presentation, which conditionally works when feedback complete:

feedback column

Update: After guidance from Zach with returning a MvcHtmlString, I had some problems being able to use this helper method above in a WebGrid Column. Below shows the correct syntax:

grid.Column(columnName: "FeedBack", format: (item) => CheckFeedBack(item.FeedBack), style: "webGridAlignment")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:59:43+00:00Added an answer on June 13, 2026 at 12:59 pm

    You must return the html not a string… for that case you can use MvcHtmlString and just raw in the view

    static System.Web.Mvc.MvcHtmlString CheckFeedBack(string item) {
            String[] feedbacks = System.Text.RegularExpressions.Regex.Split(item, "of");
    
            //if feedback complete
            if((Convert.ToInt32(feedbacks[0]) == (Convert.ToInt32(feedbacks[1]))))
            {
                string newFeedback = @"<span class=""feedBackComplete"">"+ item +"</span>";
    
                return MvcHtmlString.Create(newFeedback);
            }
            return MvcHtmlString.Create(item);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible in a .NET app (C#), to conditionally detect if a class
I am trying to set some public static constants on a class conditionally by
Is it possible to conditionally set a dependency on a gem at install time?
Possible Duplicate: Boolean types There are times when you want to conditionally set a
Is it possible to conditionally override a class property with a property method? If
Possible Duplicate: How can I conditionally compile my C# for Mono vs. Microsoft .NET?
Possible Duplicate: How to print out the method name and line number and conditionally
Is it possible to set a SetEnv variable in an .htaccess file differently depending
Possible Duplicate: Undefined Reference to I've been banging my head against this one set
Is it possible to set conditional path variables in Eclipse? This would be useful

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.