0

I have the following code which is to display an address but it does not seem to put each address row on a new line and cant figure out why

var selectedOrigoScheme = origoCedingSchemes.Where(x => x.complyingFundId.ToString() == SelectedSchemeId).ToList();
string origoName = !String.IsNullOrEmpty(selectedOrigoScheme[0].origoName) ? selectedOrigoScheme[0].origoName + Environment.NewLine : "";
string propertyName = !String.IsNullOrEmpty(selectedOrigoScheme[0].propertyName) ? selectedOrigoScheme[0].propertyName + Environment.NewLine : "";
string streetNumber = !String.IsNullOrEmpty(selectedOrigoScheme[0].streetNumber) ? selectedOrigoScheme[0].streetNumber + Environment.NewLine : "";
string street = !String.IsNullOrEmpty(selectedOrigoScheme[0].street) ? selectedOrigoScheme[0].street + Environment.NewLine : "";
string street2 = !String.IsNullOrEmpty(selectedOrigoScheme[0].street2) ? selectedOrigoScheme[0].street2 + Environment.NewLine : "";
string suburb = !String.IsNullOrEmpty(selectedOrigoScheme[0].suburb) ? selectedOrigoScheme[0].suburb + Environment.NewLine : "";
string district = !String.IsNullOrEmpty(selectedOrigoScheme[0].district) ? selectedOrigoScheme[0].district + Environment.NewLine : "";
string postcode = !String.IsNullOrEmpty(selectedOrigoScheme[0].postcode) ? selectedOrigoScheme[0].postcode + Environment.NewLine : "";
string Origoaddress = origoName + propertyName + streetNumber + street + street2 + suburb + district + postcode;
viewData["Origoaddress"] = Origoaddress;

When I debug I get the following result:

Aviva\r\nNorwich Business Capture Centre\r\nPO Box 520\r\nNorwich\r\nNR1 3WG \r\n

If I look at the result in text visualizer it displays correctly:

Aviva

Norwich Business Capture Centre

PO Box 520

Norwich

NR1 3WG


But if I look at it using HTML visualizer it displays as:

Aviva Norwich Business Capture Centre PO Box 520 Norwich NR1 3WG


Which is how its showing in my address field on the website.

What am I doing wrong as I cant see the issue.

HTML

<%=Html.TextBox("Origoaddress", ViewData["Origoaddress"].ToString())%>
murday1983
  • 3,806
  • 14
  • 54
  • 105
  • 1
    doesn't html ignore white space? That's why people feel the need to use
    to force a line break.
    – C. Knight Aug 11 '16 at 07:30
  • 1
    html viewers don't care about new line characters. If you want a new line in rendered html, you need to add something like a `
    ` tag.
    – René Vogt Aug 11 '16 at 07:30
  • Also potentially try TextArea instead of TextBox as that should support multiple lines and might work as you intend-> http://stackoverflow.com/questions/5021935/creating-multiline-textbox-using-html-helper-function – C. Knight Aug 11 '16 at 07:33
  • @C.Knight it was because I was using a TextBox and not a TextArea. Got a little code blind lol. Could you add this as an the answer so I can accept it please – murday1983 Aug 11 '16 at 07:47

1 Answers1

1

Html ignores white spaces so is ignoring the line breaks you have added. If you were working in pure html you'd need to use < br>. Html.TextBox only supports single line strings so doesn't add line breaks that Html will understand.

In order to use multiple lines you will probably need to use TextArea. See creating multiline textbox using Html.Helper function for further details.

Community
  • 1
  • 1
C. Knight
  • 739
  • 2
  • 5
  • 20