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.