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 >
<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 >
@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>
CodePudding user response:
You can use Url.Action
helper to send the line.Titolo
to your Controller
method and get your data:
<td>
<p><a href="@Url.Action("About","Home", new { titoloId = line.Titolo })">@line.Titolo</a></p>
</td>
Your Index
method will store the data in a Session
variable and then you retrieve this Session
in your About
method and cast it to the List<Libro>
type. Once you get the list, then you can filter it out based on the titolId
. Your last step would be to send this filtered list to your About
view. You need to build your About
view as per your requirement with the filtered data.
public ActionResult About(string titoloId)
{
var myFilteredList= new List<Libro>();
//Get data for titoloId
if (Session["currentLibriList"] != null)
{
var lookupList = (List<Libro>)(Session["currentLibriList"]);
myFilteredList=lookupList.Where(x=>x.Titolo == titoloId);
}
return View(myFilteredList);
}
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);
}
}
}
//Set your session here
Session["currentLibriList"]=libriList;
return View(libriList);
}