【发布时间】:2019-02-27 21:24:17
【问题描述】:
我正在处理一个列表项目,但似乎无法在我的项目中实现分页。 我尝试了在谷歌上找到的不同解决方案,但无法使其正常工作。我对 C# 或 MVC 项目没有太多经验,这是我的第一个项目。
谁能帮帮我?
这是我的模特:
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; }
}
}
这是我的控制器:
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);
}
}
}
这是我的观点:
@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>
任何帮助将不胜感激!
【问题讨论】:
-
您可以根据请求参数返回结果,作为参数请传递 size 和 pageIndex。根据这些值,您可以跳过并获取列表值。
-
请检查stackoverflow.com/questions/446196/… OtherWise你可以实现jquery数据表,这是实现数据网格分页的最简单方法,请按照此说明c-sharpcorner.com/article/…
-
你在这里展示了一大堆不相关的代码,但没有解释你的问题是什么。
标签: c# html asp.net razor asp.net-core-mvc