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 rel="nofollow" href="https://localhost:44359/Home/About" target="_parent">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);
}
}
}
return View(libriList);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
CodePudding user response:
There are many ways to parse information out of a string.
I would sugest using regex. For example with the following regex you could parse three strings separated by a *:
(?'Title'[^*] )[*](?'Author'[^*] )[*](?'ISBN'[^*] )
The Advantage of using regex is, that you check if the string is valid while the string is parsed. To test a regex I would sugest to use something like https://regex101.com
In your code you can use the C# library:
using System.Text.RegularExpressions;
Regex rx = new Regex(@"(?'Title'[^*] )[*](?'Author'[^*] )[*](?'ISBN'[^*] )",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
string text = "Harry Potter*Rowling*12345";
// Find matches.
MatchCollection matches = rx.Matches(text);
CodePudding user response:
You've switched tactic when you got to ISBN
ISBN = 4, Pagine = 5, Prezzo = 6, Quantità = 7
Assigning the editore name like this makes sense:
Editore = content[3]
It means "take the fourth element of the content
array, which is a string array, and put it in the editors property, which is a string"
I guess you'll have tried the pattern of:
ISBN = content[4]
But this won't have worked out because content
is full of strings and ISBN is an int
and even if a string purely only full of numerical chars that doesn't mean it's a number. This will have given some error like "there is no implicit conversion..."
I guess then you removed the content and just left a hard coded 4 assigned as the ISBN which will work in that it will compile syntactically, but it is incorrect logically. That will just fix every ISBN at being 4
, literally
Instead you should parse the string to an int. it's easy, and we can do it with like:
ISBN = int.Parse(content[4])
Similarly for the decimal there is a decimal.Parse
This may expose other problems like, if one of the values contains some non numerical chars (e.g. ISBNs that have hyphens in) but we can solve that later...