Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:26:59+00:00 2026-06-11T22:26:59+00:00

I am outputting an address that I would like to comma separate. If I

  • 0

I am outputting an address that I would like to comma separate.
If I don’t include any html before outputting the address it is shown correctly without any additional spaces.

Example:

97 Glen Road, Holywood, BT18 0LE

Currently Getting:

97 Glen Road , Holywood , BT18 0LE

My code is:

    <p class="no-margin-top">@Html.DisplayFor(modelItem => item.Address1)
    @if(!string.IsNullOrEmpty(item.Address2)){
        @:, @Html.DisplayFor(modelItem => item.Address2)
    }
    @if(!string.IsNullOrEmpty(item.Address3)){
        @:, @Html.DisplayFor(modelItem => item.Address3)
    }
    @if(!string.IsNullOrEmpty(item.Town)){
        @:, @Html.DisplayFor(modelItem => item.Town)
    }
    @if(!string.IsNullOrEmpty(item.County)){
        @(", ")@("Co. ")@Html.DisplayFor(modelItem => item.County)
    }
    @if(!string.IsNullOrEmpty(item.Postcode)){
        @(", ")@Html.DisplayFor(modelItem => item.Postcode)
    }
    @if(!string.IsNullOrEmpty(item.Country)){
        @(", ")@Html.DisplayFor(modelItem => item.Country)
    }

I have tried outputting the comma differently as shown above but still getting the same result.

I have also tried removing all spaces from the code as well as using @Html.Raw.

Would appreciate it if anyone has a fix for this or could suggest a better way of doing it.

UPDATE

Due to changes within the Framework Darin’s solution will now produce errors.

You will need to ensure you are using System.Linq

Then amended Darin’s code as follows:

    public string FormattedAddress
    {
        get
        {
            var values = new[] { Address1, Address2, Address3, Town, Postcode, "Co. " + County }.Where(x => !string.IsNullOrEmpty(x));
            return string.Join(", ", values);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:27:00+00:00Added an answer on June 11, 2026 at 10:27 pm

    WOW, this looks like a terrible mess you’ve ended up in your view. I would recommend you to use a view model and add the following property to your view model that will take care of properly formatting the address:

    public string FormattedAddress
    {
        get
        {
            var values = new[] { Address1, Address2, Address3, Town, Postcode, "Co. " + County }
                .Where(!string.IsNullOrEmpty);
            return string.Join(", ", values);
        }
    }
    

    and then in your view replace the mess with:

    <p class="no-margin-top">
        @Html.DisplayFor(modelItem => item.FormattedAddress)
    </p>
    

    and if now you tell me that you have violated all good practices and are not using view models, but are passing directly your domain entities to the view, well, other than telling you that this is wrong you could somehow hack it in the view:

    <p class="no-margin-top">
        @{
            var values = new[] { item.Address1, item.Address2, item.Address3, item.Town, item.Postcode, "Co. " + item.County }
                .Where(!string.IsNullOrEmpty);
            var formattedAddress = string.Join(", ", values);
        }
        @formattedAddress
    </p>
    

    But honestly go use a view model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm outputting html that's all crushed together, and would like to convert it to
I am outputting a string that consists of html content to a html file,
When outputting images from Photoshop that are over 31 characters I get a compatibility
I use TCPDF for outputting an html table, this one in my example is
I am outputting an Excel worksheet that contains strings of the form hh:mm:ss. However,
I am generating a script that is outputting information to the console. The information
I am trying to create a custom HTML Helper that encapsulates some presentation logic
I am using Ruby on Rails 3.0.7 and I would like to populate the
I currently have an array that's outputting as follows: Array ( [events_response] => Array
I am outputting a textbox to the page using the Html helpers. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.