繁体   English   中英

带有EF5的MVC 4-模型视图-代码在错误的地方?

[英]MVC 4 with EF5 - Model View - code in wrong places?

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

我的目标是创建一个显示带有父母及其相关子记录的表格的页面-父母不为每个子值重复。

父表的Column1主键

列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返回了以下内容

错误“无法将类型'string'隐式转换为'Systen.Collections.Generic.IEnumerable

我希望有人能告诉我:

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

根据我的阅读,我有以下错误:

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

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

在“视图”中,您可以像对待对象一样直接访问模型,然后尝试枚举下面的几行。

所以我建议你关注

  1. 在您的视图中插入断点并检查什么是模型
  2. 如果这不起作用,请使用Ctrl + Alt + E并标记为在每个异常时停止Visual Studio

select new VM_Trade之后添加(),直到创建VM_Trade类的新对象VM_Trade

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

我还建议您看一下Music Store应用程序教程,该教程很好地解释了这些概念。

您走在正确的道路上,只需对ViewModel和View进行少量更改即可完成。 固定结构之后,您将有一个更具体的问题,社区将很乐意回答。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM