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 7523215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:43:57+00:00 2026-05-30T02:43:57+00:00

I am using MVC 3 with Razor. I can’t figure out how to code

  • 0

I am using MVC 3 with Razor. I can’t figure out how to code a string extension regex to take this:

This_is_some_text

to display:

This is some text

I set up some enums for a drop down list, so they appear thusly (obviously I can’t create an enum with a space):

public enum MyProperty 
{
     This_is_some_text, 
     This_is_some_other_text
}

I just cannot figure out the regex is to do what I want if I am doing this:

 public static string EnumToDisplay(this string str)
        {
            return Regex.Replace(str, "[What is the regex I should use?]");
        }

As a bonus for me I would also like to add a period “.” to the end of the enum. The reason for this is that I have OCD and my enums are taking the form of short sentences. 🙂

Thanks!

  • 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-05-30T02:43:59+00:00Added an answer on May 30, 2026 at 2:43 am

    Why do you want to use regular expressions? A very wise man once said, I quote:

    Some people, when confronted with a problem, think “I know, I’ll use
    regular expressions.” Now they have two problems.

    How about using the [Display] attribute which is kinda designed exactly for this purpose:

    public enum MyProperty
    {
        [Display(Name = "This is some super text")]
        This_is_some_text,
    
        [Display(Name = "And this is some other text")]
        This_is_some_other_text
    }
    

    and then writing a custom Html helper:

    public static class HtmlExtensions
    {
        public static IHtmlString DisplayForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            if (!typeof(TProperty).IsEnum)
            {
                throw new ArgumentException("sorry this helper is inteded to be used with enum types");
            }
    
            var model = htmlHelper.ViewData.Model;
    
            if (htmlHelper.ViewData.Model == null)
            {
                return MvcHtmlString.Empty;
            }
    
    
            var field = typeof(TProperty).GetField(expression.Compile()(htmlHelper.ViewData.Model).ToString());
            if (field == null)
            {
                return new HtmlString(htmlHelper.Encode(htmlHelper.ViewData.Model.ToString()));
            }
    
            var display = field.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute;
            if (display == null)
            {
                return new HtmlString(htmlHelper.Encode(htmlHelper.ViewData.Model.ToString()));
            }
    
            return new HtmlString(htmlHelper.Encode(display.Name));
        }
    }
    

    So now assuming you have a view model:

    public class MyViewModel
    {
        public MyProperty Foo { get; set; }
    }
    

    and a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Foo = MyProperty.This_is_some_other_text
            };
            return View(model);
        }
    }
    

    you could use the custom helper we just wrote in the view to display a user friendly text that we might have associated to an ugly enum. Hey, now you could even have this working with globalization and multiple languages using resources:

    @model MyViewModel
    @Html.DisplayForEnum(x => x.Foo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MVC for REST so that I can take advantage of Razor for
Using MVC 3 Razor, how can I specify a variable within a call to
How can i directly access a .cshtml file in ASP.NET MVC using razor view
Using ASP.NET MVC's default view engine, you can declare a server-side comment like this:
I am using asp.net mvc with razor. How can I hide links which are
Using MVC-3, Razor: -- MyController -- public ActionResult Index(String message) // where message =
I'm using MVC 3 and Razor. I have a page where you can create
Using VB MVC 3 with Razor ... can I create the PayPal ipn in
Have you tried using MVC or any other UI pattern for GWT client code.
I'm using MVC 2, and the Html.EditorForModel() to allow me to display an editor

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.