0

I am in a scenario where i have a bunch of dynamic objects. They are loaded using reflection so there is no way to add code to the view that will be able to generate html to correctly render the object. How should this be done? A Razor helper seems good, but since i dont know the object on compile time, i cant really use these.

What i have done is created a base object called Person and added a virtual method called RenderHTML() in the base class.

public Medic : Person
public Cop : Person

I will then override this RenderHTML method in each class that will generate HTML that will correctly display the additional content that the child classes contain. It works, but it isnt easy to work with. Having to use a single string and add onto it is getting very messy when things get complex.

htmlstring += "some more html";

Is there any way to set some sort of output buffer where i can do something like this.

string html = startBuffer(){

<html>wdfwjewe</html>

}

the content inside the brackets could be plain HTML without anything fancy to escape it and everything will be saved to the html string. I am looking for some kind of syntax highlighting as the current method i have is becoming a pain to work with.

2
  • If the HTML is "clean", you could use an XDocument object instead of working directly with strings. Commented Apr 18, 2016 at 12:20
  • Why not let MVC handle the view generation for you? Create a partial view for each type e.g. ~/Views/Shared/DisplayTemplates/Cop.cshtml then either reference the partial view from your main view and pass in your object e.g. @Html.DisplayFor(Model.PersonObject) or, if you must have the partial as an HTML string in your server-side code, use one of the techniques for rendering a view+viewmodel to a string. Ask if you want a recommendation. Commented Apr 18, 2016 at 14:16

1 Answer 1

1

You can try to use TagBuilder() This do involve nesting of the tags if you start to build the html from <html>. Alternative would be StringBuilder()

Example is from documentation

public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)
        {
            // Create tag builder
            var builder = new TagBuilder("img");

            // Create valid id
            builder.GenerateId(id);

            // Add attributes
            builder.MergeAttribute("src", url);
            builder.MergeAttribute("alt", alternateText);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            // Render tag
            return builder.ToString(TagRenderMode.SelfClosing);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.