C# 返回空值的MVC模型
我是MVC的新手,这是我在stackoverflow中的第一个问题,请耐心听我说。我从我的客户模型中得到了一个异常。正如我的一个培训视频所示,完全相同的代码似乎在VS 2013中起作用,但在VS 2019中不起作用。有人能就如何修复此错误提出建议吗 下面是我的CustomerController代码C# 返回空值的MVC模型,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我是MVC的新手,这是我在stackoverflow中的第一个问题,请耐心听我说。我从我的客户模型中得到了一个异常。正如我的一个培训视频所示,完全相同的代码似乎在VS 2013中起作用,但在VS 2019中不起作用。有人能就如何修复此错误提出建议吗 下面是我的CustomerController代码 using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web.Mvc
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);
}
}
}
@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>
}
@model IEnumerable
@{
ViewBag.Title=“客户”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
客户
@如果(!Model.Any())
{
我们还没有顾客
}
其他的
{
顾客
成员类型
@foreach(模型中的var客户)
{
@ActionLink(customer.Name,“Details”,“Customers”,new{id=customer.id},null)
@customer.MembershipType.Name
}
}
我已经检查过了,没有发现任何问题
在控制器中:
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, 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,Name,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>
}
你到底是从哪里得到错误的?在客户视图中的@if(!Model.Any())行中,显示此错误的描述error@TomaszVizaint已经用错误描述的链接编辑了我的问题。还有这个!模型,有数据吗?检查此项,在条件语句中normaly为true或false,或者与某些数据进行比较。
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; }
}
}