A controller was developed to showcase a simple demo.
Demo Controller
Using this Controller, a collection is being sent to the View.
public class DemoController : Controller
{
// GET: Demo
public ActionResult Index()
{
List<DemoModel> list = new List<DemoModel>()
{
new DemoModel {Id = "1", Link = "One"},
new DemoModel {Id = "2", Link = "Two"},
new DemoModel {Id = "3", Link = "Three"},
new DemoModel {Id = "4", Link = "Four"}
};
return View(list);
}
}
The values passed to the next page include IDs and their corresponding links.
@model List<WebApplication9.Models.DemoModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">Panel Heading</div>
<div class="panel-body">
@for (int i = 0; i < Model.Count; i++)
{
<a href="@Url.Action("Index", "Demo2", new {@id = Model[i].Link})">
#Link to Page @Model[i].Link
</a>
<br />
}
</div>
</div>
</div>
</body>
</html>
View rendering in progress
https://i.sstatic.net/S2gyZ.png
Data is transferred to Demo2Controller where the Index Action Method accepts ID as an input.
public class Demo2Controller : Controller
{
// GET: Demo2
public ActionResult Index(string id)
{
if (!string.IsNullOrEmpty(id))
{
TempData["message"] = id;
}
else
{
TempData["message"] = "Not Clicked";
}
return View();
}
}
Index View (Demo2)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="alert alert-success">
<strong>Success!</strong> @TempData["message"]
</div>
</body>
</html>
https://i.sstatic.net/YFOQZ.png