Question

I am in the phase of building a rendering framework for rendering my models in different formats.

My idea is the following:

public class ResidenceRendererShort : IRender<Residence> {

    public string Format() {
        return "short";
    }

    public string Render(Residence content) {
        return content.Name; // Could return a whole lot of HTML
    }   
}

I can have multiple of those with different formats, and they are all injected using Ninject DI into my RenderingService, where I got methods for finding the correct render, using methods like e.g. FindRendererFor(Type type, string format)

Now my question is, how can I create a tag in razor which will use the rendering service and applying the correct render? I have been looking into HtmlHelpers, but they are static methods and I can not inject my RenderingService into this.

I thought I could create something like:

@Model my.namespace.Residence
@Html.RenderObject(Model, "short");

Am I missing something or someone got an idea on how to accomplish this?

Was it helpful?

Solution

You're killing yourself. Just use Display/Editor Templates. If you have a view in ~/Views/Shared/DisplayTemplates or ~/Views/Shared/EditorTemplates named after your class, Residence.cshtml in this case, then Razor will use this view to render your class whenever it's passed to Html.DisplayFor or Html.EditorFor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top