C# MVC4和EF5-模型视图-代码在错误的位置?

C# MVC4和EF5-模型视图-代码在错误的位置?,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我通常使用SQL和SSR,并且是C#的新手,正在从事我的第一个MVC项目 我的目标是创建一个页面,显示一个包含父项及其关联子项记录的表,而不需要父项为每个子项值重复 列1父表的主键 列2父表中的名称 第3列子名称列表 为此,我尝试创建一个模型视图 我相信我在代码放置方面犯了很多错误 模型视图 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Syst

我通常使用SQL和SSR,并且是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>
控制器视图

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>
查看

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>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.TaxpayerID) @DisplayNameFor(model=>model.Supplier) @DisplayNameFor(model=>model.EhSupplierNumber) @DisplayNameFor(model=>model.SupplierTradingName1) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.TaxpayerID) @DisplayFor(modelItem=>item.Supplier) @DisplayFor(modelItem=>item.EhSupplierNumber) @DisplayFor(modelItem=>item.SupplierTradingName1) @ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})| @ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/}) }
  • 最初,我试图将IEnumerable放在视图模型中,我的控制器LINQ返回以下结果
错误“无法将类型“string”隐式转换为“Systen.Collections.Generic.IEnumerable”

我希望有人能告诉我:

  • 如果模型视图和控制器的结构不正确
  • 如何为父列返回单个值
  • 根据我所读到的,我有以下错误:

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

    我已经读了几天了,似乎什么都可以用。

    在你看来,你首先要像访问对象一样访问模型,然后下面几行就开始列举它

    所以我建议你遵循

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

  • 在创建
    VM\u Trade
    类的新对象之前,请在
    选择新VM\u Trade
    后添加()。

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

    我还建议您看看应用程序教程,它很好地解释了这些概念

    您走的是正确的道路,只需对ViewModel和View进行一些更改,就可以完成。一旦结构修复,您将有更具体的问题,社区将乐于回答


    编辑:这只是一点帮助,有一个Nuget软件包“EntityFramework.Sample”,它为您提供了示例模型和一个演示DbContext集供您使用。

    感谢您的建议-尽管音乐商店确实很有帮助,但仍在工作。