【问题标题】:Problem doing Multiple Join for Index View Controller为索引视图控制器进行多重连接时出现问题
【发布时间】:2019-07-20 10:17:06
【问题描述】:

我想在我的索引控制器中使用连接,以便我的索引视图具有来自规范化表的用户友好数据。

我正在努力用 MVC5 做到这一点,因为它对我来说是新的。下面是我的索引控制器的代码。它不起作用,我知道我缺少某些东西或有什么问题 - 但我不知道是什么。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ICS20web.Models;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;
using System.Text;

namespace ICS20web.Controllers
{
    public class InternalOrdersController : Controller
    {
          private ICS_Internal_Transactions db = new ICS_Internal_Transactions();

    // GET: InternalOrders
    public ActionResult Index()
    {

        var q = from tr in db.ICS_Transactions

                join un in db.ICS_Units 
                on tr.DeliveryUnitID equals un.DeliveryUnitID


                 join kt in db.ICS_Contacts 
                 on tr.Contact equals kt.LoginID

                 join sp in db.ICS_Supplies on tr.SuppliesID equals sp.Supplies_ID

                 select new { sp.ItemDescription, tr.OriginalDate, tr.TransType, tr.LastUpdatedBy , kt.ContactName, tr.OpenClosed, tr.CurrentStatus, tr.CurrentStatusDate, tr.RequsitionNumber, tr.PONumber, tr.DeliveryMonth, tr.DeliveryYear, tr.UnitsOrdered, tr.Emergency, tr.Comments, un.DeliveryUnit, un.PhoneNumber, un.Street, un.City, un.State, un.ZipCode, un.Building, un.Room};



        return View(q.ToList());


     //   return View(db.ICS_Transactions.ToList());
    }

这是真正的错误

传入字典的模型项的类型为'System.Collections.Generic.List1[<>f__AnonymousType523[System.String,System.Nullable1[System.DateTime],System.String,System.String,System.String,System.String,System.String,System.Nullable1[System.DateTime],System.String,System.String ,System.String,System.Nullable1[System.Int32],System.Nullable1[System.Int32],System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String, System.String,System.String]]',但此字典需要类型为“System.Collections.Generic.IEnumerable`1[ICS20web.Models.ICS_Transactions]”的模型项。

【问题讨论】:

  • 究竟是什么不工作?您是否遇到任何运行时错误或编译错误?
  • @Stormhashe - 我用(运行时)错误和更多控制器代码更新了我的问题。
  • 显示您的视图文件

标签: c#


【解决方案1】:

您视图中的模型需要 IEnumerable<ICS20web.Models.ICS_Transactions> 或类似的类型,但您将匿名对象传递给您的视图。

您需要创建一个 ViewModel 类,然后将视图上的模型更改为该类。

public class IndexViewModel
{
    public string ItemDescription {get; set;}
    public DateTime OriginalDate {get; set;}
    /* ... additional properties ...*/
}

然后更改您的查询:

var q = from tr in db.ICS_Transactions
        join un in db.ICS_Units on tr.DeliveryUnitID equals un.DeliveryUnitID
        /* ... */
        select new IndeViewModel{ 
                  ItemDescrition = sp.ItemDescription, 
                  OriginalDate = tr.OriginalDate, 
                  /* ... */
        };

最后在您的 Index.cshtml(或任何您的视图)中将模型声明更改为

 @model YournameSpace.IndexViewModel

【讨论】:

  • 谢谢@Marco。这正是我所需要的。这很简单。我现在明白了,为什么它不起作用。您的解决方案解决了我的问题。
【解决方案2】:

您正在选择一种与您的视图不同的新对象类型(匿名对象)

这部分select new { sp.ItemDescription, tr.OriginalDate, tr.TransType, tr.LastUpdatedBy , kt.ContactName, tr.OpenClosed, tr.CurrentStatus, tr.CurrentStatusDate, tr.RequsitionNumber, tr.PONumber, tr.DeliveryMonth, tr.DeliveryYear, tr.UnitsOrdered, tr.Emergency, tr.Comments, un.DeliveryUnit, un.PhoneNumber, un.Street, un.City, un.State, un.ZipCode, un.Building, un.Room};

【讨论】:

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