Reputation: 565
I have been searching for about 20 minutes, with no luck on answering this question. I would very much appreciate anyone's input on this.
I have one cell (td) inside of a table row, and that cell will contain one line of text most of the time, but will occasionally contain two lines (the exact text that it contains depends on server-side processing). I don't mind so much if the text of one line is mushed together and wraps around, but I need to be able to start a new line if a second line is added. Hence my attempts to use the newline character below.
I can't really break it up into two <td>
elements, and I can't seem to get \n
or <br/>
to work when I include them in the text server-side. If I do that, they are just rendered as plain text inside the cell.
Essentially, I want to be able to avoid analyzing the text with javascript client-side to add special characters. I am not even sure if that would work. Is there some css/html/javascript/jquery trick that might do the job?
The C# code that builds the text is simple enough, here's one example:
caseDueDate = "Reading Response due: " + readingDueDate.ToString();
The Razor code for the field looks like this:
<td>@Model.patients.ElementAt(ii).CaseDue</td>
The correctness of the results coming back is just fine. T'is only the formatting... =)
Upvotes: 1
Views: 12920
Reputation: 1696
You're using ASP.NET MVC on the server, right?
Two options spring to mind, if you want to manually control the use of line breaks on the server side:
(1) You could write a display template (partial view) for CaseDue property. If you mark your model with the UIHint attribute, like so:
[UIHint("MyPartialView")]
public string CaseDue { ... }
... and then use an HtmlHelper in your view, e.g.:
@Html.DisplayFor(m => m.patients.ElementAt(index).CaseDue)
(2) You could write an extension method for the HtmlHelper class which creates and returns an instance of MvcHtmlString. Razor doesn't encode these, so you could include whatever tags you want in the return value. e.g., roughly:
public static MvcHtmlString TdForCaseDue(this HtmlHelper html, string[] lines) {
TagBuilder td = new TagBuilder("td");
td.InnerHtml = lines[0];
for(int i = 1; i < lines.Length; i++) {
td.InnerHtml += (new TagBuilder("br").ToString());
td.InnerHtml += lines[i];
}
return new MvcHtmlString(td.ToString());
}
Upvotes: 1
Reputation: 171
BR tags withing TDs should work... the server-side code is probably automatically escaping the html to prevent cross-site scripting. There are different ways to selectively disable this depending on the specific server software being used.
Upvotes: 1
Reputation: 3368
Sounds like something is converting your <br />
tags to htmlentities at some point in your code, as these should not render as plain text in html output.
Is your output wrapped in an htmlentities()
call or something similar?
Upvotes: 1