带有 EF5 的 MVC 4 - 模型视图 - 代码位于错误的位置

本文关键字:于错误 错误 位置 视图 MVC EF5 模型 带有 代码 | 更新日期: 2023-09-27 18:30:49

我通常使用 SQL 和 SSRS,并且是 C# 的新手,正在处理我的第一个 MVC 项目。

我的目标是创建一个页面,显示一个包含父项及其关联子记录的表,而无需父项为每个子值重复。

列 1 父表中的主键

第 2 列 父表中的名称

第3栏 子名列表

为此,我尝试创建一个模型视图。

我相信我犯了许多与代码放置相关的错误

模型视图

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace SupplierItemTest.Models
{
    public class VM_Trade
    {
        [Required]
        [Display(Name = "Tax Info")]
        public virtual string TaxpayerID { get; set; }
        [Required]
        [Display(Name = "Entity Name")]
        public virtual string Supplier { get; set; }
        [Required]
        [Display(Name = "Trading Names")]
        public virtual string SupplierTradingName1 { get; set; }
        [Required]
        [Display(Name = "Supplier Number")]
        public virtual string EhSupplierNumber { get; set; }
    }
}

控制器视图

    using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SupplierItemTest.Models;

namespace SupplierItemTest.Controllers
{
    public class TradingNameController : Controller
    {
        private SupplierItemEntities db = new SupplierItemEntities();
        //
        // GET: /TradingName/
        public ActionResult Index()
        {
            var Trade_View =
                from s in db.Suppliers
                join st in db.SupplierTradingNames on s.TaxpayerID equals st.TaxpayerID
                join sn in db.EhSuppliers on s.TaxpayerID equals sn.TaxpayerID
                orderby s.TaxpayerID
                select new VM_Trade
                 {
                     SupplierTradingName1 = st.SupplierTradingName1,
                     TaxpayerID = s.TaxpayerID,
                     Supplier = s.SupplierName,
                     EhSupplierNumber = sn.EhSupplierNumber
                      };                     
            return View(Trade_View.ToList());

视图

@model IEnumerable<SupplierItemTest.Models.VM_Trade>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
           @Html.DisplayNameFor(model => model.TaxpayerID)
        </th>
        <th>
           @Html.DisplayNameFor(model => model.Supplier)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.EhSupplierNumber)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.SupplierTradingName1)
        </th>
        <th></th>
    </tr> 
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.TaxpayerID)
       </td>
        <td>
             @Html.DisplayFor(modelItem => item.Supplier)
        </td>
         <td>
           @Html.DisplayFor(modelItem => item.EhSupplierNumber)
        </td>
        <td>
           @Html.DisplayFor(modelItem => item.SupplierTradingName1)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}
</table>
  • 最初我尝试将 IEnumerable 放置在视图模型中,我的控制器 LINQ 返回了以下内容

错误"无法将类型'字符串'隐式转换为'Systen.Collections.Generic.IEnumerable

我希望有人能告诉我:

  1. 如果模型视图和控制器的结构不正确
  2. 如何返回父列的单个值

从我所读到的内容来看,我有以下非常错误:

  • 应在模型视图中使用 IEnumerable
  • LINQ 不应使用联接,因为数据库中存在关系
  • 视图不应包含 IEnumerable
  • 我不知道如何从父列返回单个值和子行的多个值。

我已经阅读了几天,似乎可以得到任何东西。

带有 EF5 的 MVC 4 - 模型视图 - 代码位于错误的位置

您的视图模型和视图存在问题。您可以在此处参考类似情况的基本结构。

我还建议您查看音乐商店应用程序教程,该教程很好地解释了这些概念。

您走在正确的道路上,只需对视图模型和视图进行少量更改,您就会完成。一旦结构固定,您将有更具体的问题,社区将很乐意回答。

编辑:只是一点帮助,有这个Nuget包"EntityFramework.Sample",它为您提供示例模型和一个演示DbContext设置。

在你的视图中,你首先访问你的模型,就像它是一个对象,然后你下面的几行试图枚举它。

所以我建议你遵循

  1. 在视图中插入断点并检查模型是什么
  2. 如果这没有帮助,请使用Ctrl + Alt + E并标记以在每个异常上停止Visual Studio。

select new VM_Trade后添加(),因为您要创建VM_Trade类的新对象。