'how to create links between pages?
public class Libro
{
public string Titolo { get; set; }
public string Autore { get; set; }
public string Editore { get; set; }
public int ISBN { get; set; }
public int Pagine { get; set; }
public decimal Prezzo { get; set; }
public int Quantità { get; set; }
public Libro BuildLibro(string input)
{
Libro result = null;
if (!String.IsNullOrEmpty(input))
{
var inputArray = input.Split('*');
if (inputArray.Length >= 6)
{
result = new Libro();
result.Titolo = inputArray[0];
result.Autore = inputArray[1];
result.Editore = inputArray[2];
if (!string.IsNullOrEmpty(inputArray[3]))
{
int.TryParse(inputArray[3], out int num);
result.ISBN= num;
}
if (!string.IsNullOrEmpty(inputArray[4]))
{
int.TryParse(inputArray[4], out int num);
result.Pagine = num;
}
if (!string.IsNullOrEmpty(inputArray[5]))
{
decimal.TryParse(inputArray[5], out decimal num);
result.Prezzo = num/100;
}
if (!string.IsNullOrEmpty(inputArray[6]))
{
int.TryParse(inputArray[6], out int num);
result.Quantità = num;
}
}
}
return result;
}
}
}
in the Index.cshtml
<table class="table">
<tr>
<th>
Titolo
</th>
<th>
Autore
</th>
<th>
Editore
</th>
<th>
Prezzo(€)
</th>
</tr>
@foreach (var line in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => line.Titolo)
<p> <a href="@Url.Action("About","Home", new { titoloId = line.Titolo })"> Visualizza la scheda </a></p>
</td>
<td>
@Html.DisplayFor(modelItem => line.Autore)
</td>
<td>
@Html.DisplayFor(modelItem => line.Editore)
</td>
<td>
@Html.DisplayFor(modelItem => line.Prezzo)
</td>
</tr>
}
</table>
</body>
</html>
I have created a model of a book based on a list of a file.txt in order to display a table with all the books available, their author, publisher and price, now, for each book, I should be able to open a descriptive sheet also containing ISBN , pages and quantities. From paragraph in the html below @Html.DisplayFor(modelItem => line.Titolo) I inserted a link to the About.cshtml, but I don't know what code to write inside and what to write in the controller too?
this is my controller at the moment:
public ActionResult Index()
{
var fileInput = Reader.Read("C:/Users/test/source/repos/Books/Books/App_Data/Libri.txt");
var libriList = new List<Libro>();
if (fileInput != null)
{
for (var i = 1; i < fileInput.Count; i++)
{
var libri = new Libro();
libri = libri.BuildLibro(fileInput[i]);
if (libri != null)
{
libriList.Add(libri);
}
}
}
Session["currentLibriList"] = libriList;
return View(libriList);
}
public ActionResult About(string titoloId)
{
var myFilteredList = new List<Libro>();
if (Session["currentLibriList"] != null)
{
var lookupList = (List<Libro>)(Session["currentLibriList"]);
myFilteredList = (List<Libro>)lookupList.Where(x => x.Titolo == titoloId);
}
return View(myFilteredList);
}
and this is About.cshtml
<html>
<body>
<table class="table">
@foreach (var line in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => line.Titolo)
</td>
<td>
@Html.DisplayFor(modelItem => line.Autore)
</td>
<td>
@Html.DisplayFor(modelItem => line.Editore)
</td>
<td>
@Html.DisplayFor(modelItem => line.Prezzo)<p>€</p>
</td>
<td>
@Html.DisplayFor(modelItem => line.Pagine)
</td>
<td>
@Html.DisplayFor(modelItem => line.Quantità)
</td>
<td>
@Html.DisplayFor(modelItem => line.ISBN)
</td>
</tr>
}
</table>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
