Reputation: 669
How to use @Html.DisplayFor(...) to display a list of data, separated by comma.
For example, Model:
public class Player
{
public int Id { get; set; }
public List<int> scores { get; set; }
}
If player.scores contains data 8,9,10.
@Html.DisplayFor(model => model.scores);
Output will be like: 8910;
Is it possible to display the data separated by comma, like 8,9,10 ?
Upvotes: 1
Views: 3992
Reputation:
There is no point using @Html.DisplayFor(...)
in your case (and it cannot work anyway because an expression cannot contain a method to convert the value).
DisplayFor()
is useful when you are using a custom DisplayTemplate
, or you have a [DisplayAttribute]
applied to the property (and the property is DateTime
or a numeric type which you want to format in a particular way).
If you do want to use DisplayFor()
, then you must add another property to your view model, say
public string Score { get; set; }
and in the GET method you would set its value by using
model.Score = String.Join(", ", model.scores);
and in the view
@Html.DisplayFor(m => m.Score)
Alternatively, you can just use
@String.Join(", ", model.scores)
in the view directly (no DisplayFor()
)
Upvotes: 4