问题描述
Error Error 我对MVC还是很陌生,这是我在stackoverflow中的第一个问题,所以请耐心等待。我一直在从客户模型中获取ArgumentNullException。就像我的一个培训视频中所示,完全相同的代码似乎在VS 2013中有效,但在我的VS 2019中却无效。有人可以建议如何解决此错误吗?
下面是我的CustomersController代码
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Vidly.Models;
namespace Vidly.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext _context;
public CustomersController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
public ViewResult Index()
{
/* Eager Loading */
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
return View(customers);
}
public ActionResult Details(int id)
{
var customer = _context.Customers.SingleOrDefault(c => c.Id == id);
if (customer == null)
return HttpNotFound();
return View(customer);
}
}
}
And this is my Customers View.
@model IEnumerable<Vidly.Models.Customer>
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (!Model.Any())
{
<p>We don't have any customers yet.</p>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Membership Type</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name,"Details","Customers",new { id = customer.Id },null)</td>
<td>@customer.MembershipType.Name</td>
</tr>
}
</tbody>
</table>
}
解决方法
我检查了,没发现问题。
在控制器中:
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 Vidly.Data;
using Vidly.Models;
namespace Vidly.Controllers
{
public class CustomersController : Controller
{
private readonly ApplicationDbContext _context;
public CustomersController(ApplicationDbContext context)
{
_context = context;
}
// GET: Customers
//public async Task<IActionResult> Index()
//{
// return View(await _context.Customer.ToListAsync());
//}
public ViewResult Index()
{
/* Eager Loading */
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
return View(customers);
}
// GET: Customers/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var customer = await _context.Customers
.FirstOrDefaultAsync(m => m.Id == id);
if (customer == null)
{
return NotFound();
}
return View(customer);
}
// GET: Customers/Create
public IActionResult Create()
{
return View();
}
// POST: Customers/Create
// To protect from overposting attacks,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,Name,MembershipType")] Customer customer)
{
if (ModelState.IsValid)
{
_context.Add(customer);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(customer);
}
// GET: Customers/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customer = await _context.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
}
return View(customer);
}
// POST: Customers/Edit/5
// To protect from overposting attacks,see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id,[Bind("Id,MembershipType")] Customer customer)
{
if (id != customer.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(customer);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomerExists(customer.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(customer);
}
// GET: Customers/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var customer = await _context.Customers
.FirstOrDefaultAsync(m => m.Id == id);
if (customer == null)
{
return NotFound();
}
return View(customer);
}
// POST: Customers/Delete/5
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var customer = await _context.Customers.FindAsync(id);
_context.Customers.Remove(customer);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool CustomerExists(int id)
{
return _context.Customers.Any(e => e.Id == id);
}
}
}
视图中:
@model IEnumerable<Vidly.Models.Customer>
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (!Model.Any())
{
<p>We don't have any customers yet.</p>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Membership Type</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name,"Details","Customers",new { id = customer.Id },null)</td>
<td>@customer.MembershipType</td>
</tr>
}
</tbody>
</table>
}
在模型中
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Vidly.Models
{
public class Customer
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public MembershipType MembershipType { get; set; }
}
}