Home How to display enum attribute in Engine.Razor.RunCompile method
Reply: 0

How to display enum attribute in Engine.Razor.RunCompile method

user2825
1#
user2825 Published in June 20, 2018, 1:26 am

I have a model it defines some properties that are enums. This is one of them

 public enum NutritionalRequirementValue
{
    Regular,
    [EnumMember(Value = "Mechanicla Soft")]
    Mechanicla_Soft,
    [EnumMember(Value = "Heart Healthy")]
    Heart_Healthy,
    [EnumMember(Value = "Low Cholesterol")]
    Low_Cholesterol,
    [EnumMember(Value = "Low Fat")]
    Low_Fat,
    [EnumMember(Value = "Sodium Restriction")]
    Sodium_Restriction,
    [EnumMember(Value = "No Added Salt")]
    No_Added_Salt,
    [EnumMember(Value = "Calorie ADA Diet")]
    Calorie_ADA_Diet,
    [EnumMember(Value = "No Concentrated Sweets")]
    No_Concentrated_Sweets,
    [EnumMember(Value = "Coumadin Diet")]
    Coumadin_Diet,
    [EnumMember(Value = "Renal Diet")]
    Renal_Diet,
    [EnumMember(Value = "Enteral Nutrition")]
    Enteral_Nutrition,
    TPN,
    Supplements,
    [EnumMember(Value = "Fluid Restriction")]
    Fluid_Restriction,
    other
}  

What I want to do is to use Razor engine to generate html by passing the cshtml file path and model. I'm using this method to do it

   private static readonly HashSet<string> RazorCache = new HashSet<string>();
    private static readonly string BaseDirectory = HttpRuntime.AppDomainAppPath;
    public static string Render<T>(T viewModel, string cshtmlFile)
    {
        cshtmlFile = Path.Combine(BaseDirectory, cshtmlFile);
        if (!File.Exists(cshtmlFile))
        {
            throw new ArgumentException(string.Format("The given cshtml file '{0}' must exist.", cshtmlFile));
        }
        var key = string.Format("key-{0}-{1}-{2}", cshtmlFile, File.GetLastWriteTimeUtc(cshtmlFile).Ticks, typeof(T));

        string result;
        if (!RazorCache.Contains(key))
        {
            var cshtml = File.ReadAllText(cshtmlFile);
            result = Engine.Razor.RunCompile(new LoadedTemplateSource(cshtml, cshtmlFile), key, typeof(T), viewModel);
            RazorCache.Add(key);
        }
        else
        {
            result = Engine.Razor.Run(key, typeof(T), viewModel);
        }
        return result;
    }

But now, the generated html has enum value for example "Mechanicla_Soft" instead of "Mechanicla Soft". That underscore is ugly in front end. My question is how to get "Mechanicla Soft"? I tried to use Jsonconvert to serialize it with StringEnumConverter(), it is fine in the serialized Json, as I can see the value is "Mechanicla Soft", but once I deserialize it back to the model type, it becomes "Mechanicla_Soft" again.

 var modelStr = JsonConvert.SerializeObject(viewData, new Newtonsoft.Json.Converters.StringEnumConverter());
            var model = JsonConvert.DeserializeObject<PocViewData>(modelStr);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301533 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO