[英]Problem with Foreign keys displaying on asp.net mvc core index pages after scaffolding
我最近将我的一些 Asp.net mvc5 应用程序升级到了 asp.net mvc core 3。我在索引页面上显示外键时遇到问题。 它们在 mvc 5 中显示良好,但未在 mvc 核心中显示。 我将包括相关的代码文件。 三个外键分别是 Department、Project 和 Solicitation Type。 任何想法都会有所帮助。
谢谢。
请求控制器.cs
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 TOGSolicitations.Models;
namespace TOGSolicitations.Controllers
{
public class SolicitationsController : Controller
{
private readonly TOGProjectsContext _context;
public SolicitationsController(TOGProjectsContext context)
{
_context = context;
}
// GET: Solicitations
public async Task<IActionResult> Index()
{
Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<Solicitations, SolicitationType> tOGProjectsContext = _context.Solicitations.Include(s => s.Department).Include(s => s.Project).Include(s => s.SolicitationType);
return View(await tOGProjectsContext.ToListAsync());
}
// add Search ablity on the Index Page
[HttpGet]
public async Task<IActionResult> Index(String Solicitationsearch)
{
ViewData["GetSolicitationDetails"] = Solicitationsearch;
var solicitationquery = from x in _context.Solicitations select x;
if (!String.IsNullOrEmpty(Solicitationsearch))
{
solicitationquery = solicitationquery.Where(x => x.SolicitationNumber.Contains(Solicitationsearch) || x.SolicitationDescription.Contains(Solicitationsearch));
}
return View(await solicitationquery.AsNoTracking().ToListAsync());
}
// GET: Solicitations/Details/5
public async Task<IActionResult> Details(short? id)
{
if (id == null)
{
return NotFound();
}
var solicitations = await _context.Solicitations
.Include(s => s.Department)
.Include(s => s.Project)
.Include(s => s.SolicitationType)
.FirstOrDefaultAsync(m => m.SolicitationId == id);
if (solicitations == null)
{
return NotFound();
}
return View(solicitations);
}
// GET: Solicitations/Create
public IActionResult Create()
{
ViewData["DepartmentId"] = new SelectList(_context.Departments, "DepartmentId", "DepartmentName");
ViewData["ProjectId"] = new SelectList(_context.Projects, "ProjectId", "ProjectDescription");
ViewData["SolicitationTypeId"] = new SelectList(_context.SolicitationType, "SolicitationTypeId", "SolicitationTypeDescription");
return View();
}
// POST: Solicitations/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("SolicitationId,FiscalYear,SolicitationTypeId,SolicitationNumber,DepartmentId,SolicitationDescription,BidDate,VendorSelected,SolicitationPrice,PurchaseOrderNumber,EngineeringProjectNumber,ProjectId")] Solicitations solicitations)
{
if (ModelState.IsValid)
{
_context.Add(solicitations);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["DepartmentId"] = new SelectList(_context.Departments, "DepartmentId", "DepartmentName", solicitations.DepartmentId);
ViewData["ProjectId"] = new SelectList(_context.Projects, "ProjectId", "ProjectDescription", solicitations.ProjectId);
ViewData["SolicitationTypeId"] = new SelectList(_context.SolicitationType, "SolicitationTypeId", "SolicitationTypeDescription", solicitations.SolicitationTypeId);
return View(solicitations);
}
// GET: Solicitations/Edit/5
public async Task<IActionResult> Edit(short? id)
{
if (id == null)
{
return NotFound();
}
var solicitations = await _context.Solicitations.FindAsync(id);
if (solicitations == null)
{
return NotFound();
}
ViewData["DepartmentId"] = new SelectList(_context.Departments, "DepartmentId", "DepartmentName", solicitations.DepartmentId);
ViewData["ProjectId"] = new SelectList(_context.Projects, "ProjectId", "ProjectDescription", solicitations.ProjectId);
ViewData["SolicitationTypeId"] = new SelectList(_context.SolicitationType, "SolicitationTypeId", "SolicitationTypeDescription", solicitations.SolicitationTypeId);
return View(solicitations);
}
// POST: Solicitations/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(short id, [Bind("SolicitationId,FiscalYear,SolicitationTypeId,SolicitationNumber,DepartmentId,SolicitationDescription,BidDate,VendorSelected,SolicitationPrice,PurchaseOrderNumber,EngineeringProjectNumber,ProjectId")] Solicitations solicitations)
{
if (id != solicitations.SolicitationId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(solicitations);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SolicitationsExists(solicitations.SolicitationId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["DepartmentId"] = new SelectList(_context.Departments, "DepartmentId", "DepartmentName", solicitations.DepartmentId);
ViewData["ProjectId"] = new SelectList(_context.Projects, "ProjectId", "ProjectDescription", solicitations.ProjectId);
ViewData["SolicitationTypeId"] = new SelectList(_context.SolicitationType, "SolicitationTypeId", "SolicitationTypeDescription", solicitations.SolicitationTypeId);
return View(solicitations);
}
// GET: Solicitations/Delete/5
public async Task<IActionResult> Delete(short? id)
{
if (id == null)
{
return NotFound();
}
var solicitations = await _context.Solicitations
.Include(s => s.Department)
.Include(s => s.Project)
.Include(s => s.SolicitationType)
.FirstOrDefaultAsync(m => m.SolicitationId == id);
if (solicitations == null)
{
return NotFound();
}
return View(solicitations);
}
// POST: Solicitations/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(short id)
{
var solicitations = await _context.Solicitations.FindAsync(id);
_context.Solicitations.Remove(solicitations);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool SolicitationsExists(short id)
{
return _context.Solicitations.Any(e => e.SolicitationId == id);
}
}
}
征集
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace TOGSolicitations.Models
{
public partial class Solicitations
{
public short SolicitationId { get; set; }
[DisplayName("Fiscal Year")]
public short FiscalYear { get; set; }
[DisplayName("Solicitation Type")]
public short SolicitationTypeId { get; set; }
[DisplayName("Solicitation Number")]
[Required, StringLength(25)]
public string SolicitationNumber { get; set; }
[DisplayName("Department")]
public short DepartmentId { get; set; }
[DisplayName("Solicitation Description")]
[Required, StringLength(80)]
[RegularExpression(@"(([A-za-z0-9\s\-]+))$")]
public string SolicitationDescription { get; set; }
[DisplayName("Bid Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? BidDate { get; set; }
[DisplayName("Vendor Selected")]
[StringLength(80)]
public string VendorSelected { get; set; }
[DisplayName("Solicitation Price")]
[RegularExpression(@"(([A-za-z0-9\s\-\.]+))$")]
public decimal? SolicitationPrice { get; set; }
[DisplayName("Purchase Order Number")]
public string PurchaseOrderNumber { get; set; }
[DisplayName("Enginnering Project Number")]
[StringLength(10)]
[RegularExpression(@"(([A-za-z0-9\s\-]+))$")]
public string EngineeringProjectNumber { get; set; }
[DisplayName("Project Description")]
public int? ProjectId { get; set; }
public virtual Departments Department { get; set; }
public virtual Projects Project { get; set; }
public virtual SolicitationType SolicitationType { get; set; }
}
}
征集索引.cshtml
@model IEnumerable<TOGSolicitations.Models.Solicitations>
@{
ViewData["Title"] = "Solicitations";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div style="=font-family:Arial">
<h1>Solicitations</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<form method="get" asp-action="Index">
<p>
<input type="search" placeholder="Enter Solicitation Number or Solicitation Description ..." value="@ViewData["GetSolicitationDetails"]" name="Solicitationsearch" style="width:500px;" />
<input type="submit" value="Search" class="btn btn-primary" />
<a asp-action="Index">Get All Solicitations</a>
</p>
</form>
<table class="table" border="1">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FiscalYear)
</th>
<th>
@Html.DisplayNameFor(model => model.SolicitationNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.SolicitationDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.BidDate)
</th>
<th>
@Html.DisplayNameFor(model => model.EngineeringProjectNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Department)
</th>
<th>
@Html.DisplayNameFor(model => model.Project)
</th>
<th>
@Html.DisplayNameFor(model => model.SolicitationType)
</th>
<th width="100">Action </th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FiscalYear)
</td>
<td>
@Html.DisplayFor(modelItem => item.SolicitationNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.SolicitationDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.BidDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EngineeringProjectNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.DepartmentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project.ProjectDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.SolicitationType.SolicitationTypeDescription)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.SolicitationId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.SolicitationId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.SolicitationId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
当您访问 Solicitations Index.cshtml 时,会调用带有参数的 index 操作,您可以使用断点来检查 .So 更改您的 Index 操作,如下所示:
[HttpGet]
public async Task<IActionResult> Index(String Solicitationsearch)
{
ViewData["GetSolicitationDetails"] = Solicitationsearch;
var solicitationquery = _context.Solicitation
.Include(s => s.Department)
.Include(s => s.Project)
.Include(s => s.SolicitationType)
.AsQueryable();
if (!String.IsNullOrEmpty(Solicitationsearch))
{
solicitationquery = solicitationquery.Where(x => x.SolicitationNumber.Contains(Solicitationsearch) || x.SolicitationDescription.Contains(Solicitationsearch));
}
return View(await solicitationquery.AsNoTracking().ToListAsync());
}
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.