Display HTML table from comma separated value in ASP.Net MVC

nabilabolo
 
on Oct 12, 2021 12:31 AM
Sample_189062.zip
453 Views

Hi,

i want to display a html table based on the list that i have and i don't know how to separate the row value by and show it in their column .

the list is

List<fieldname> field= new List<fieldname>();
List<tagno> tag = new List<tagno>();

the data of field is (should be column header)

(i) Tag No

(ii) Grade

(iii) Name

(iv) Address

the data of tag is (row value)

(i) 101 - 200, A , Lily , England

(ii) 201 - 300, B, Smith , London

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 12, 2021 06:01 AM

Hi nabilabolo,

Refer below code.

Model

public class tagno
{
    public string data { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        List<tagno> tag = new List<tagno>();
        tag.Add(new tagno { data = "101 - 200, A , Lily , England" });
        tag.Add(new tagno { data = "201 - 300, B, Smith , London" });
        return View(tag);
    }
}

View

@model List<Commasepareted_Table_MVC.Models.tagno>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table>
        <tr>
            <th>Tag No</th>
            <th>Grade</th>
            <th>Name</th>
            <th>Address</th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                @for (int i = 0; i < item.data.Split(',').Length; i++)
                {
                    <td>@item.data.Split(',')[i]</td>
                }
            </tr>
        }
    </table>
</body>
</html>

Screenshot