Hi sani.ss501,
Check the below exmple.
Model
public class StudentCategories
{
public string Category { get; set; }
public List<string> Names { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
var categories = GetCategories().Distinct().ToList();
List<StudentCategories> scs = new List<StudentCategories>();
foreach (var item in categories)
{
scs.Add(new StudentCategories
{
Category = item.title,
Names = GetStudents().Where(x => x.idCat == item.id).Select(x => x.name).ToList()
});
}
return View(scs);
}
public List<tbl_category> GetCategories()
{
List<tbl_category> categories = new List<tbl_category>();
categories.Add(new tbl_category { id = 1, title = "IT" });
categories.Add(new tbl_category { id = 2, title = "Software" });
return categories;
}
public List<tbl_student> GetStudents()
{
List<tbl_student> students = new List<tbl_student>();
students.Add(new tbl_student { id = 1, idCat = 1, name = "ali" });
students.Add(new tbl_student { id = 2, idCat = 1, name = "reza" });
students.Add(new tbl_student { id = 3, idCat = 2, name = "maryam" });
students.Add(new tbl_student { id = 4, idCat = 2, name = "many" });
return students;
}
public class tbl_category
{
public int id { get; set; }
public string title { get; set; }
}
public class tbl_student
{
public int id { get; set; }
public int idCat { get; set; }
public string name { get; set; }
}
}
View
@model IEnumerable<StudentCategories>
@using Group_Wise_Data_MVC.Models;
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@foreach (StudentCategories sc in Model)
{
<h4>@sc.Category :</h4>
<table>
@foreach (string name in sc.Names)
{
<tr>
<td>@name</td>
</tr>
}
</table>
}
</body>
</html>
Screenshot