-1

I want to display the content of a Json file in a view in a project using ASP.NET MVC, but when i get the data and display it in a table in a view it looks like:

"data1data2data3data4"

How i can separate that JSON array of string with commas like:

"data1 , data2 , data3 , data4"

The JSON file has the next structure:

[  
   {  
      "Name":"Value",
      "Name2":[  
         "Data1",
         "Data2",
         "Data3",
         "Data4",

      ]
   }
]

And i display the data in a table like:

<table border="1">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Name2</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var element in Model)
                {
                    <tr>
                        <td>@Html.DisplayFor(m => element.Name)</td>
                        <td>@Html.DisplayFor(m => element.Name2)</td>
                    </tr>
                }
            </tbody>
</table>

But it looks like:

Name  | Name2
-----------------------------
Value | Data1Data2Data3Data4

and i want to display the data like:

Name  | Name2
-----------------------------
Value | Data1 , Data2 , Data3 , Data4

Any way to do this?

Edit:

I have this method to get the Json data:

string fileJson = File.ReadAllText(@"Path.json");
var dataJson = 
JsonConverter.DeserializeObject<List<Object>>(fileJson);
return dataJson;

And then called it from a controller

7
  • Can you show your C# code where you parse JSON to C# objects? Commented Sep 2, 2019 at 22:48
  • @JohanP Html.DisplayFor() helper displays a model property, it can't be used for custom string. Commented Sep 2, 2019 at 23:03
  • Sure, i added the code that i use to get the Json data Commented Sep 2, 2019 at 23:11
  • Possible duplicate of Convert Array List to Comma Separated String in C# Commented Sep 2, 2019 at 23:20
  • <th colspan="4">Name2</th> Commented Sep 3, 2019 at 0:38

2 Answers 2

1

Replace your second td with:

<td>@Html.Raw(string.Join(" , ", element.Name2))</td>
Sign up to request clarification or add additional context in comments.

Comments

1

you can do m => string.Join(" , ", element.Name2) to get the format you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.