Im working on a list project, and can't seem to implement pagination in my project. I've tried different solutions I've found on google, but couldn't get it to work. I do not have much experience with C# or MVC projects at all, this is my first project.
Could anyone help me?
This is my Model:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MvcMovie.Models
{
public class funnwpweb01
{
public int ID { get; set; }
[Display(Name = "Domenenavn")]
[StringLength(250)]
[Required]
public string Domain { get; set; }
[Display(Name = "IP-Adresse")]
[StringLength(250)]
[Required]
public string IP { get; set; }
[Display(Name = "Server")]
[StringLength(250)]
[Required]
public string Server { get; set; }
[Display(Name = "Opprettet")]
[DataType(DataType.Date)]
public DateTime Start_Date { get; set; }
[Display(Name = "Pris")]
[DataType(DataType.Currency)]
[Required]
public string Price { get; set; }
[Display(Name = "Plattform")]
[StringLength(250)]
[Required]
public string Platform { get; set; }
[Display(Name = "Firma")]
[StringLength(250)]
[Required]
public string Firm { get; set; }
}
}
This is my Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using MvcMovie.Models;
namespace MvcMovie.Controllers
{
public class Funnwpweb01Controller : Controller
{
private readonly FunnWebsitesContext _context;
public Funnwpweb01Controller(FunnWebsitesContext context)
{
_context = context;
}
// GET: funnwpweb01
// Requires using Microsoft.AspNetCore.Mvc.Rendering;
public async Task<IActionResult> Index(string server, string search, string platform)
{
// Use LINQ to get list of genres.
IQueryable<string> genreQuery = from m in _context.Listings
orderby m.Server
select m.Server;
var funnwpweb01 = from m in _context.Listings
select m;
if (!String.IsNullOrEmpty(search))
{
funnwpweb01 = funnwpweb01.Where(s => s.Domain.Contains(search));
}
if (!String.IsNullOrEmpty(server))
{
funnwpweb01 = funnwpweb01.Where(x => x.Server == server);
}
var movieGenreVM = new DomainServerViewModel();
movieGenreVM.servers = new SelectList(await genreQuery.Distinct().ToListAsync());
movieGenreVM.funnwpweb01 = await funnwpweb01.ToListAsync();
return View(movieGenreVM);
}
// GET: funnwpweb01/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var funnwpweb01 = await _context.Listings
.FirstOrDefaultAsync(m => m.ID == id);
if (funnwpweb01 == null)
{
return NotFound();
}
return View(funnwpweb01);
}
// GET: funnwpweb01/Create
public IActionResult Create()
{
return View();
}
// POST: funnwpweb01/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Domain,IP,Server,Start_Date,Price,Platform,Firm")] funnwpweb01 funnwpweb01)
{
if (ModelState.IsValid)
{
_context.Add(funnwpweb01);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(funnwpweb01);
}
// GET: funnwpweb01/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var funnwpweb01 = await _context.Listings.FindAsync(id);
if (funnwpweb01 == null)
{
return NotFound();
}
return View(funnwpweb01);
}
// POST: funnwpweb01/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Domain,IP,Server,Start_Date,Price,Platform,Firm")] funnwpweb01 funnwpweb01)
{
if (id != funnwpweb01.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(funnwpweb01);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Funnwpweb01Exists(funnwpweb01.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(funnwpweb01);
}
// GET: funnwpweb01/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var funnwpweb01 = await _context.Listings
.FirstOrDefaultAsync(m => m.ID == id);
if (funnwpweb01 == null)
{
return NotFound();
}
return View(funnwpweb01);
}
// POST: funnwpweb01/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var funnwpweb01 = await _context.Listings.FindAsync(id);
_context.Listings.Remove(funnwpweb01);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool Funnwpweb01Exists(int id)
{
return _context.Listings.Any(e => e.ID == id);
}
}
}
And this is my view:
@model MvcMovie.Models.DomainServerViewModel
@{
ViewData["Title"] = "Funn Webhotell";
}
<h2>Funn Webhotell</h2>
<p>
<a asp-action="Create">Opprett ny</a>
</p>
<form asp-controller="funnwpweb01" asp-action="Index">
<p>
IP/Server: <select asp-for="Server" asp-items="Model.servers">
<option value="">All</option>
</select>
Domenenavn: <input type="text" name="search">
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th width="15%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].Firm)
</th>
<th width="15%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].Domain)
</th>
<th width="15%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].IP)
</th>
<th width="15%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].Server)
</th>
<th width="10%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].Start_Date)
</th>
<th width="10%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].Price)
</th>
<th width="10%">
@Html.DisplayNameFor(model => model.funnwpweb01[0].Platform)
</th>
<th width="10%">
Detaljer
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.funnwpweb01)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Firm)
</td>
<td>
<a href="http://@Html.DisplayFor(modelItem => item.Domain)">@Html.DisplayFor(modelItem => item.Domain)</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.IP)
</td>
<td>
@Html.DisplayFor(modelItem => item.Server)
</td>
<td>
@Html.DisplayFor(modelItem => item.Start_Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Platform)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Endre</a> |
<a asp-action="Details" asp-route-id="@item.ID">Detaljer</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Slett</a>
</td>
</tr>
}
</tbody>
</table>
Any help would be highly appreciated!