Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept ViewData and ViewBag as Anonymous object #38

Open
cadilhac opened this issue Apr 20, 2022 · 1 comment
Open

Accept ViewData and ViewBag as Anonymous object #38

cadilhac opened this issue Apr 20, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@cadilhac
Copy link

cadilhac commented Apr 20, 2022

Hi,

Found your library after coming from RazorLight for which I was so disappointed by the author who has been condescending and unpleasant relatively to a simple question I was asking.
I thought such library was quite difficult to write but I found yours is such easy to use and feature complete (views where you expect them to be, Layout, ViewImports)... Really, this is a great job. Thank you for this work.

I just wanted to let you know some simple addition I made to have a more straightforward ViewBag that I could set up as an anonymous object. Maybe this is something you will want to add in your lib. I did it with a static wrapper:

public static class RazorTemplateEngineWrapper
{
    public async static Task<string> RenderAsync<TModel>([DisallowNull] string viewName, [DisallowNull] TModel model, [DisallowNull] object viewBag)
    {
        return await RazorTemplateEngine.RenderAsync(viewName, model, AnonymousTypeToDictionary(viewBag));
    }

    private static Dictionary<string, object> AnonymousTypeToDictionary(object obj)
    {
        var result = new Dictionary<string, object>();

        if (obj != null)
        {
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj))
            {
                result.Add(pd.Name, pd.GetValue(obj));
            }
        }

        return result;
    }
}

Note that instead of Dictionary<string, object>, if the code was using a IDictionary<string, object?> it would be more exact I think.

I'm using it like this:

var body = await RazorTemplateEngineWrapper.RenderAsync("~/Views/EmailTemplates/EmailConfirm.text.cshtml", model, new { Message = message });

Thanks again.

@soundaranbu
Copy link
Owner

Hi @cadilhac, glad to know that you find this library helpful and easy to use.

and for your suggestion, yes, it's a good to have feature and will be more intuitive for the users. I'm thinking of implementing it in the upcoming versions.

Keep supporting. Thanks!

@soundaranbu soundaranbu added the enhancement New feature or request label May 8, 2022
@soundaranbu soundaranbu changed the title So easy to use, and a suggestion Accept ViewData and ViewBag as Anonymous object Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants