This is my controller :
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page)
{
var user = await _userManager.GetUserAsync(HttpContext.User);
ViewData["CurrentSort"] = sortOrder;
ViewData["GenreSortParm"] = string.IsNullOrEmpty(sortOrder) ? "genre_desc" : "";
ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
ViewData["CurrentFilter"] = searchString;
var userBooks = _userService.PagedUserBooks(user, sortOrder, currentFilter, searchString, page);
var result = await PaginatedList<Book>.CreateAsync(userBooks, page ?? 1, 5);
return View(result);
}
The "userBooks" is an IQueryable so that in "CreateAsync" it can be passed and the "result" that I pass into the View is PaginatedList
In my View I am using a @model PaginatedList<Book>
and here comes the issue in this part of my View
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@item.Genre
</td>
<td>
@item.Title
</td>
<td>
@item.CreatedOn
</td>
<td>
<a asp-controller="Book" asp-action="MakeReview" asp-route-id="@item.Id" class="btn btn-success">Add Review</a>
</td>
</tr>
}
</tbody>
I use asp-route-id="@item.Id"
which is the Id of the item, which is Book so that I can pass the Id to the action "MakeReview" as you see specified in the <a asp-controller="Book" asp-action="MakeReview" asp-route-id="@item.Id" class="btn btn-success">Add Review</a>
But when I go to my action method
[HttpGet]
public async Task<IActionResult> MakeReview(int bookId)
{
var user = await _userManager.GetUserAsync(HttpContext.User);
var book = await _bookService.GetById(user, bookId);
//Not fully implemented
return View();
}
the int bookId passed as a parameter should be the Id of the book I chose to Review, but it is always 0. Does this have something to do with using the PaginatedList as a model where inside the class I don't have a property of Id? This is my class "PaginatedList"
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex < TotalPages);
}
}
public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
UPDATE: When the paramater in the action MakeReview is of type Book and not int and I pass the ID which is int to the Action then the modelbinding works, of course everything is null except the Id.
asp-route-id="@item.Id"
should beasp-route-bookId="@item.Id"
orint bookId
should beint id
. As it stands, there's no match betweenid
andbookId
.