脚手架安装后,asp.net mvc核心索引页上显示外键的问题

Bashtonmcse

我最近将一些Asp.net mvc5应用程序升级到了asp.net mvc core3。我在索引页上显示外键时遇到问题。它们在mvc 5中显示正常,但不在mvc core中显示。我将包括相关的代码文件。这三个外键分别是“部门”,“项目”和“请求类型”。任何想法都会有所帮助。

谢谢。

SolicitationsController.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>
Xueli Chen

当您访问Solicitations Index.cshtml时,将调用带有参数的index操作,您可以使用断点进行检查。因此,如下所示更改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());
    }

结果:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

重新安装Microsoft Visual Studio Asp.net MVC 5脚手架扩展

来自分类Dev

ASP.NET MVC 5-多对一关系的脚手架

来自分类Dev

Asp.Net Core 3.0 MVC-文化Cookie的脚手架身份页面路由

来自分类Dev

asp.net mvc core 2 脚手架生成控制器中的错误

来自分类Dev

在 ASP.net Core MVC 2 中使用 ASP.net MVC 5 脚手架

来自分类Dev

在带有EF的脚手架控制器ASP.NET MVC的索引方法中添加where子句

来自分类Dev

ASP.NET MVC 5自定义脚手架选项[t4模板]

来自分类Dev

带有脚手架编辑控制器的新ASP.net核心项目仅返回404页面未找到

来自分类Dev

如何在asp.net MVC核心中设置身份脚手架项目/页面如何初始页面?

来自分类Dev

使用ASP.NET Core 2.1 MVC中的Entity Framework Core脚手架新控制器时出现问题

来自分类Dev

.net MVC 脚手架模板:检查模型是否包含属性

来自分类Dev

为什么在使用数据库优先的方法进行脚手架安装后,ASP.net CORE中的CRUD剃须刀页面无法立即使用?

来自分类Dev

ASP.NET VNext中的命令行脚手架

来自分类Dev

如何使用EF Core更新ASP.NET 5中的dbcontext脚手架?

来自分类Dev

VS 2019中ASP.NET Core Razor脚手架模板的位置是什么

来自分类Dev

在ASP.Net Core中使用Entity Framework Core脚手架时出错

来自分类Dev

HiddenInput 不适用于 ASP.NET Core 2.2 脚手架

来自分类Dev

具有外键约束的脚手架实体的实体框架核心问题

来自分类Dev

ASP.NET MVC 5-脚手架为多对一关系

来自分类Dev

脚手架身份框架mvc核心

来自分类Dev

实体框架核心-自定义脚手架

来自分类Dev

实体框架核心不会让我脚手架

来自分类Dev

Rails脚手架索引过滤

来自分类Dev

上下文菜单不包含脚手架VS2015 ASP.NET 5

来自分类Dev

上下文菜单不包含脚手架VS2015 ASP.NET 5

来自分类Dev

Grails 3脚手架问题

来自分类Dev

MVC 4无法执行脚手架

来自分类Dev

MVC脚手架中始终需要日期字段

来自分类Dev

Ruby on Rails脚手架未显示

Related 相关文章

  1. 1

    重新安装Microsoft Visual Studio Asp.net MVC 5脚手架扩展

  2. 2

    ASP.NET MVC 5-多对一关系的脚手架

  3. 3

    Asp.Net Core 3.0 MVC-文化Cookie的脚手架身份页面路由

  4. 4

    asp.net mvc core 2 脚手架生成控制器中的错误

  5. 5

    在 ASP.net Core MVC 2 中使用 ASP.net MVC 5 脚手架

  6. 6

    在带有EF的脚手架控制器ASP.NET MVC的索引方法中添加where子句

  7. 7

    ASP.NET MVC 5自定义脚手架选项[t4模板]

  8. 8

    带有脚手架编辑控制器的新ASP.net核心项目仅返回404页面未找到

  9. 9

    如何在asp.net MVC核心中设置身份脚手架项目/页面如何初始页面?

  10. 10

    使用ASP.NET Core 2.1 MVC中的Entity Framework Core脚手架新控制器时出现问题

  11. 11

    .net MVC 脚手架模板:检查模型是否包含属性

  12. 12

    为什么在使用数据库优先的方法进行脚手架安装后,ASP.net CORE中的CRUD剃须刀页面无法立即使用?

  13. 13

    ASP.NET VNext中的命令行脚手架

  14. 14

    如何使用EF Core更新ASP.NET 5中的dbcontext脚手架?

  15. 15

    VS 2019中ASP.NET Core Razor脚手架模板的位置是什么

  16. 16

    在ASP.Net Core中使用Entity Framework Core脚手架时出错

  17. 17

    HiddenInput 不适用于 ASP.NET Core 2.2 脚手架

  18. 18

    具有外键约束的脚手架实体的实体框架核心问题

  19. 19

    ASP.NET MVC 5-脚手架为多对一关系

  20. 20

    脚手架身份框架mvc核心

  21. 21

    实体框架核心-自定义脚手架

  22. 22

    实体框架核心不会让我脚手架

  23. 23

    Rails脚手架索引过滤

  24. 24

    上下文菜单不包含脚手架VS2015 ASP.NET 5

  25. 25

    上下文菜单不包含脚手架VS2015 ASP.NET 5

  26. 26

    Grails 3脚手架问题

  27. 27

    MVC 4无法执行脚手架

  28. 28

    MVC脚手架中始终需要日期字段

  29. 29

    Ruby on Rails脚手架未显示

热门标签

归档