【问题标题】:MVC 4 with EF5 - Model View - code in wrong places?带有 EF5 的 MVC 4 - 模型视图 - 代码在错误的位置?
【发布时间】:2013-09-11 04:08:15
【问题描述】:

我通常使用 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 放在 View Model 中,我的控制器 LINQ 返回以下内容

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

我希望有人能告诉我:

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

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

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

我已经阅读了几天,似乎可以做任何事情。

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

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

    我还建议你看看Music Store 应用教程,它很好地解释了这些概念。

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

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

    【讨论】:

    • 感谢您的建议 - 尽管音乐商店的工作确实很有帮助。
    【解决方案2】:

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

    所以我建议你关注

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多