0

Right now I have in my Mvc4 project a .cs class named HTMLcreator in my App_Code folder, which makes html for me to render in my views.

This I've heard is not the best way, and it's tiring to update the html elements in the .cs file.

What whould be a better way to create dynamic html for the view to use?


HTMLcreator.cs:

public static string methodOne()
    {
        StringBuilder result = new StringBuilder();
        List<Subjects> subjects = Subjects.GetAll(); // Gets a some objects

        foreach (Subjects s in subjects)
        {
            result.Append("<p>" + s.name + "</p>");

            if(s.hasChild)
            {
                result.Append(methodTwo(Subjects.GetChild(s)));
            }
        }
        return Convert.ToString(result);
    }

public static string methodTwo(Subjects s)
    {
        StringBuilder result = new StringBuilder();

        result.Append("<p>" + s.name + "</p>");

        if(s.hasChild)
        {
            result.Append(methodTwo(Subjects.GetChild(s)));
        }
        return Convert.ToString(result);
    }

The View calls the method with a @Html.Raw method.

Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89
  • 2
    How about using Views in the way they were intended? Views allow scaffolding of the web page, and give you the ability to inject values into them. You seem to be taking the approach of generating the *entire* web page and injecting that. In ASP.NET MVC 4 they even give you a nice syntax called Razor for accomplishing this. Why reinvent an already nice wheel? – Robert Harvey Nov 18 '13 at 18:24
  • 1
    I have no idea what you are talking about. Do you not use html views? – asawyer Nov 18 '13 at 18:24
  • 1
    Why `Convert.ToString(result)` instead of `result.ToString()`? – haim770 Nov 18 '13 at 18:28
  • @asawyer & Robert Harvey: I am using views. I just have some code where I need to loop thru a list of objects, and generate some html based on that. Can I do something similar only using views? ´cause that would be a solution ^^ – Michael Tot Korsgaard Nov 18 '13 at 18:40
  • 2
    @MichaelTotKorsgaard That sounds exactly like what I would do with an Edit/Display template partial view. – asawyer Nov 18 '13 at 19:06
  • 1
    @MichaelTotKorsgaard: You can create loops in the view, directly. See here for an example: http://stackoverflow.com/q/1160420/102937 – Robert Harvey Nov 18 '13 at 20:14
  • @RobertHarvey: I know. But what about a loop calling itself, like my code does? – Michael Tot Korsgaard Nov 18 '13 at 21:18
  • 1
    That's just ordinary recursion. You can do the same in View code, with a bit of ingenuity. However, see here: http://stackoverflow.com/q/942489. – Robert Harvey Nov 18 '13 at 21:19
  • 1
    See also http://weblogs.asp.net/raduenuca/archive/2011/04/24/asp-net-mvc-displaying-a-tree-view-using-a-recursive-declarative-helper-and-jquery.aspx – Robert Harvey Nov 18 '13 at 21:24
  • 1
    And http://weblogs.asp.net/jigardesai/archive/2008/02/04/display-hierarchical-data-in-asp-net-mvc-framework.aspx – Robert Harvey Nov 18 '13 at 21:24

1 Answers1

2

What you actually need here is a helper:

@helper methodOne(Subjects subjects)
{
    foreach (var subject in subjects)
    {
        <p>@subject.name</p>

        if (subject.hasChild)
            @methodOne(Subjects.GetChild(subject))
    }
}

Then use it somewhere in your View:

<div>
      @methodOne(subjects)
</div>

Yet, if you insist on generating the HTML yourself, your better use System.Web.Mvc.TagBuilder:

Contains classes and properties that are used to create HTML elements. This class is used to write helpers, such as those found in the System.Web.Helpers namespace.

See MSDN.

haim770
  • 48,394
  • 7
  • 105
  • 133