如何将多个表中的列值传递给MVC中的视图?

时间:2015-05-26 20:24:55

标签: asp.net-mvc linq entity-framework

我正在显示来自名为gigs的表格中的数据,但是它包含几个表格的外键和“乐队”。和'场地'所以在我的控制器中使用此代码时,

            string user = User.Identity.GetUserId();

            var yourgigs = (from g in dbg.gigs
                                   from v in dbg.Venues
                                   from b in dbg.Bands
                                   from ga in g.gigsaccasses
                                   where (ga.Id == user &&
                                   v.venueid == g.venueid &&
                                   b.bandid == g.bandid)
                                   select g);

            return View(yourgigs);

它在视图中显示bandid和venueid是无意义的整数。我如何用我认为b.bandname,v.venuename以及添加v.address1和v.city来替换它们?执行此操作的SQL语句是

SELECT        bands.bandname, venues.venuename, venues.address1, venues.city, gigs.whatdate, gigs.starttime
FROM            gigs INNER JOIN
                         bands ON gigs.bandid = bands.bandid INNER JOIN
                         gigsaccass ON gigs.gigid = gigsaccass.gigid INNER JOIN
                         dbo.AspNetUsers ON gigsaccass.Id = dbo.AspNetUsers.Id INNER JOIN
                         venues ON gigs.venueid = venues.venueid
                         WHERE dbo.AspNetUsers.Id = //some user//

我确实尝试过使用匿名类型:



 var yourgigs = (from g in dbg.gigs
                            from v in dbg.Venues
                            from b in dbg.Bands
                            from ga in g.gigsaccasses
                            where (ga.Id == user &&
                            v.venueid == g.venueid &&
                            b.bandid == g.bandid
                            select new
                            {
                                bandname = b.bandname,
                                venuename = v.venuename,
                                address1 = v.address1,
                                city = v.city,
                                whatdate = g.whatdate,
                                starttime = g.starttime
                            });




但是这引发了一个错误:

传递到字典中的模型项的类型是&#39; System.Data.Entity.Infrastructure.DbQuery 1[<>f__AnonymousType7 6 [System.String,System.String,System.String,System.String,System .DateTime,System.TimeSpan]]&#39;,但是这个字典需要一个类型为&#39; System.Collections.Generic.IEnumerable`1 [OnStageTonight_MVC.Models2.gigs]&#39;的模型项。

View正在期待类型&#39; gigs&#39;

@model IEnumerable<OnStageTonight_MVC.Models2.gigs>

@{
    ViewBag.Title = "Gigs";
}

<h2>Gigs</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.venueid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.bandid)
        </th>

我错过了什么?

编辑:

我应该补充一点,我确实有一个模型,但我认为这是错误的。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace OnStageTonight_MVC.Models2
{

    [Table("gigs")]
    public partial class gigs
    {
        public gigs()
        {
            this.gigsaccasses = new HashSet<gigsaccass>();
        }

        [Key]
        public int gigid { get; set; }
        public int venueid { get; set; }
        public int bandid { get; set; }
        [Display(Name="Date")]
        public System.DateTime whatdate { get; set; }
        [Display(Name="Starts at")]
        public System.TimeSpan starttime { get; set; }

        public virtual ICollection<gigsaccass> gigsaccasses { get; set; }
    }

    [Table("gigsaccass")]
    public partial class gigsaccass
    {
        [Key]
        public int gigaccassid { get; set; }
        public int gigid { get; set; }
        public string Id { get; set; }

        public virtual gigs gig { get; set; }
        public virtual AspNetUsers AspNetUser { get; set; }
    }

    [Table("dbo.AspNetUsers")]
    public class AspNetUsers
    {
        [Key]
        public string Id { get; set; }
        public string Email { get; set; }
        public bool EmailConfirmed { get; set; }
        public string PasswordHash { get; set; }
        public string SecurityStamp { get; set; }
        public string PhoneNumber { get; set; }
        public bool PhoneNumberConfirmed { get; set; }
        public bool TwoFactorEnabled { get; set; }
        public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
        public bool LockoutEnabled { get; set; }
        public int AccessFailedCount { get; set; }
        public string UserName { get; set; }
        public string YourName { get; set; }

        public List<gigsaccass> gigsaccasses { get; set; }
    }

    [Table("venues")]
    public partial class venues
    {
        [Key]
        public int venueid { get; set; }
        [Required]
        [Display(Name = "Venue")]
        public string venuename { get; set; }        
        [Required]
        [Display(Name = "Address")]
        public string address1 { get; set; }
        [Required]
        [Display(Name = "City")]
        public string city { get; set; }
        public List<gigs> venuegigs { get; set; }
    }

    [Table("bands")]
    public class bands
    {
        [Key]
        public int bandid { get; set; }
        [Required]
        [Display(Name = "Name")]
        public string bandname { get; set; }
        public List<gigs> bandgigs { get; set; }
    }

    public partial class gigscontext : DbContext
    {
        public gigscontext()
            : base("DefaultConnection")
        {
        }

        public DbSet<gigs> gigs { get; set; }
        public DbSet<gigsaccass> gigsaccass { get; set; }
        public DbSet<AspNetUsers> AspNetUsers { get; set; }
        public DbSet<venues> Venues { get; set; }
        public DbSet<bands> Bands { get; set; }
    }
}

3 个答案:

答案 0 :(得分:0)

您无法在此处使用匿名课程。您的视图需要知道如何使用模型,它需要类型信息,但是:&#34;类型名称由编译器生成,在源代码级别不可用&#34;

你无法传递这些物品。 http://www.codeproject.com/Articles/15624/Inside-C-Anonymous-Methods#5

您需要创建表示数据集中的行并返回已填充对象列表的类。

作为首选方案,您可以使用dynamichttps://msdn.microsoft.com/en-us/library/dd264736.aspx

答案 1 :(得分:0)

MVC中的M代表模型,你想要的是MVC的3个主要租户之一。

您希望一个对象封装要在视图上显示的所有信息。

答案 2 :(得分:0)

最佳做法是为视图创建模型。这是一个附加层,用于将项目(您的实体模型)的存储与其显示分开。

 var yourgigs = (from g in dbg.gigs
                            from v in dbg.Venues
                            from b in dbg.Bands
                            from ga in g.gigsaccasses
                            where (ga.Id == user &&
                            v.venueid == g.venueid &&
                            b.bandid == g.bandid
                            select new GigViewModel
                            {
                                bandname = b.bandname,
                                venuename = v.venuename,
                                address1 = v.address1,
                                city = v.city,
                                whatdate = g.whatdate,
                                starttime = g.starttime
                            });

public class GigViewModel
{
    public string bandname { get; set; }
    public string venuename { get; set; }
    public string address1 { get; set; }
    public string city { get; set; }
    public DateTime whatdate { get; set; }
    public Timespan starttime { get; set; }
}

从视图模型中保留数据时,请使用AutoMapper或类似的东西来复制视图模型和实体模型之间具有匹配名称的属性。

在视图中使用新视图模型:
@model IEnumerable<OnStageTonight_MVC.Models2.GigModelView>