Concatenate a string for DisplayFor

拜拜、爱过 提交于 2020-01-06 07:00:31

问题


I have a model called Lines. On it I have a address class that contains a number of strings, i.e.:

public string ReferenceKey { get; set; }
public string Country { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
public string PremisesName { get; set; }
public string PremisesName { get; set; }

I get information back from a webservice call to an outside party and then populate these fields with the returned data - note in some cases they may not return everything so County or PostTown for example may be returned empty.

Currently in my cshtml page I am displaying the address as below:

@Html.DisplayFor(model => model.Address.ReferenceKey),
@Html.DisplayFor(model => model.Address.PremisesName),
@Html.DisplayFor(model => model.Address.PostTown),
@Html.DisplayFor(model => model.Address.Postcode),
@Html.DisplayFor(model => model.Address.County),
@Html.DisplayFor(model => model.Address.Country)

It works fine when all data is returned however if some fields are Blank it would show for e.g - REF1,,,POSTCODE,County,Country - i.e the fields it doesnt have a value for wont be printed but the commas will which doesnt look very good. My idea was to add another string to my model like below.

public string ConcatAddress { get; set; }

Now were I am kind of stuck - in my controller I was doing the below to build up the string:

model.ConcatAddress = model.Address.ReferenceKey + model.Address.PremisesName....etc, etc

What would I have to do to replace the double commas with one, etc depending on if the value. A string.IsNullorEmpty before each value check perhaps but what on the replace?


回答1:


Add the strings that you want in a list, e.g.:

var str = New List<string>();
if (!string.IsNullOrEmpty(model.Address.ReferenceKey)) {
  str.Add(model.Address.ReferenceKey);
}

Then join the strings:

return string.Join(",", str);



回答2:


You can do it with a combination of String.Join and a Linq query. The approach below could be made into an extension method on string very easily. You could also use Linq's Aggregate function, but to my mind, String.Join is more intuitive.

This solution will also ensure you don't need to worry about leading or trailing commas.

// Jon all the non-empty address components together.
model.ConcatAddress = string.Join(
    ", ", 
    (new string[] {
        model.Address.ReferenceKey,
        model.Address.PremisesName,
        model.Address.PostTown,
        model.Address.Postcode,
        model.Address.County,
        model.Address.Country
    }).
    Where(s => !string.IsNullOrEmpty(s)).
    ToArray());



回答3:


You need to map the DisplayFor argument to a property in the model, say FullAddress, so you can add a property like:

public string FullAddress 
{ 
    get
    {
        string[] addressParts = { ReferenceKey, Country, County, Postcode, PremisesName }
        return string.Join(",", addressParts.Where(s => !string.IsNullOrEmpty(s)).ToArray());
    }  
}

And do:

@Html.DisplayFor(model => model.FullAddress)



回答4:


You can add a property like this to your model

public string FullAddress
{
    get
    {
        return new List<string>
            { 
                Address.ReferenceKey,
                Address.PremisesName,
                Address.PostTown,
                Address.PostCode,
                Address.County,
                Address.Country
            }
            .Where(s => !string.IsNullOrWhiteSpace(s))
            .Aggregate((s1, s2) => s1 + "," + s2);
    }
}

I am assuming that by blank fields you mean empty or whitespace, otherwise replace IsNullOrWhiteSpace with IsNullOrEmpty.



来源:https://stackoverflow.com/questions/11844080/concatenate-a-string-for-displayfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!