0

I have the following code :

Model code :

public class StudentViewModel
{        
    public string FirstAddress { get; set; }
    public string SecondAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string FullAddress
    {
        get
        {
            if (!string.IsNullOrEmpty(FirstAddress) && !string.IsNullOrEmpty(SecondAddress))
            {
                return $"{Address1}{Environment.NewLine}{Address2}{Environment.NewLine}{City}, {State} {Zipcode}";
            }
            else
            {
                return $"{Address1}{Environment.NewLine}{City}, {State} {Zipcode}";
            }
        }
        set { this.FullAddress = value; }
    }

}

This is how it's populated.

var record = context.StudentAdresses.Where(w => w.Active).Select(s => new {new StudentViewModel{FirstAddress =s.Address1, SecondAddress =s.Address2, City=s.City, State=s.State, Zipcode =s.Zipcode}).ToList();

View :

@model List<StudentViewModel>

@if (Model != null)
{
    <div>
        <table>
            <thead>
                <tr>
                    <th>Address</th>
                </tr>
            </thead>

            <tbody>

                @foreach (var record in Model)
                {
                    <tr>
                        <td>
                           @record.FullAddress
                        </td>
                    </tr>
                }
             </tbody>
        </table>
    </div>
}

But the Full name does not appear with new line. Instead I get a single line of Address.

410 Address1 City, CA 32130

where as I need:

410 Address1
City, CA 32130
TheFallenOne
  • 1,598
  • 2
  • 23
  • 57
  • it seems a simple `$"...\r\n..."` in your string will solve the problem. – Alireza0772 May 16 '20 at 14:53
  • 9
    Bear in mind that HTML ignores line breaks unless you explicitly add a `
    ` tag.
    – Powerlord May 16 '20 at 14:55
  • 2
    Does this answer your question? [Line break in HTML with '\n'](https://stackoverflow.com/questions/39325414/line-break-in-html-with-n) – Progman May 16 '20 at 14:56
  • @Progman white-space: pre-line while works fine with my string; adds an extra blank line at the top. – TheFallenOne May 16 '20 at 15:28
  • @Powerlord so do you mean I need to add
    in the view and can't do anything in my model?
    – TheFallenOne May 16 '20 at 15:30
  • Btw, the `set`ter of your property is recursive and will cause a Stackoverflow if you ever tried to use it. I would think that it should not even exist since the `get`ter is calculated from other properties – pinkfloydx33 May 16 '20 at 16:55

2 Answers2

1

Correct answer is from @Powerlord above. Html does replace newlines by a simple whitespace. So you need to replace newline with the html <br /> tag to enforce a line break. That comment should be the right answer

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Daniel Schmid
  • 647
  • 6
  • 12
  • so do you mean I need to add
    in the view and can't do anything in my model on the server side?
    – TheFallenOne May 16 '20 at 15:35
  • @TheFallenOne: No you don't have to do so but I would. I think the presentation of data is the job of the view and not of the model. I would pass an address object to the view and do the formatting there. – Daniel Schmid May 16 '20 at 15:42
-1

it is work for me

change lie below:

add br tag before {City}

  return $"{Address1}{Environment.NewLine}{"<br/>"}{City}, {State} {Zipcode}";

in razor page :

   @Html.Raw(@record.FullAddress)