I am working on a Restful application that uses mvc and razor to return xml (let's skip right past how this is a bad idea)
I have a foreach within a partial view that generates xml nodes based on the item:
@foreach (var item in Model)
{
@Html.RenderElement(item.Type.ToString(), item.Value)
}
RenderElement Helper:
public static string RenderElement(this HtmlHelper helper, string elementName, object elementValue)
{
return string.Format("<{0}>{1}</{0}>", elementName, helper.Encode(elementValue));
}
I am seeing two issues with this for each loop
- The items are all being placed on the same line in the output
- The output of RenderElement is being html encoded
For the first issue is there an accepted method of forcing razor to render a new line?
Let me reiterate that I am NOT outputting html with this razor. So a <br>
tag is not an option.
I can fix the second issue by wrapping the RenderElement call in a call to @Html.Raw() but this feels messy, so ideally I would like to understand why it is being encoded in the first place and if there is a cleaner way to prevent that.