I have a MySQL database from which I am creating a list and then using a @foreach to display in a table. I currently have a "0" as a placeholder in my table, but I need the first one to be 1, then 2, etc. I don't want to use the existing key because I filter the list (selectedUser) & want the list to always show the number of entries, not how to reference it in the database.
@foreach (var u in Users.Where(d => d.User == selectedUser))
{
<tr>
<td title="">0</td>
<td>@u.User</td>
<td>@u.UserLevel</td>
<td>(you get the idea)</td>
</tr>
}
Pertinent part of my @code block:
List<DBTestModel> Users;
protected override async Task OnInitializedAsync()
{
string sql = "select * from MyTable";
Users = await _data.LoadData<DBTestModel, dynamic>(sql, new { }, _config.GetConnectionString("default"));
}
I'm still pretty new to this (and this is my first foray into development beyond very simple UI), so please forgive a question that does get asked a fair bit; I did a lot of Googling & found many answers, but I couldn't apply any directly to this situation (I think because people just included the part of theirs that was broken & I needed to see the whole of it to understand it). I believe the pieces I need are:
var i = 0;
and
i++;
but I'm just not seeming to get them in the right place.
Something like:
@{ var x = 1; }
@foreach (var u in Users.Where(d => d.User == selectedUser))
{
<tr>
<td title="">@x</td>
<td>@u.User</td>
<td>@u.UserLevel</td>
<td>(you get the idea)</td>
</tr>
x++;
}
My actual test code looked like this:
@page "/Test"
@{ var x = 1;}
@foreach (var c in Cities)
{
<div> @x - @c</div>
x++;
}
@code {
private List<string> Cities => new List<string>() { "Adijan", "Lagos", "Tripoli" };
}
I just transposed the question code in.