2012-11-28 66 views
1

我發現這個項目/教程:MVC網格到Excel

http://www.codeproject.com/Articles/325103/MVC-Grid-to-Excel-file-download

而且它的偉大工程!但是,我只能讓它與一個模型一起工作。所以,如果我只是拉動我的組織模型,那麼效果很好。但是,我需要通過運行SQL查詢來填充GridView。我認爲LINQ to SQL可以工作,但我無法弄清楚。

這會從我的組織模型中加載數據並將其發送到視圖。

private VAGTCEntities db = new VAGTCEntities(); 
public ActionResult Index() 
{ 
    ViewBag.Message = "Welcome to ASP.NET MVC!"; 
    List<Organization> model = db.Organizations.ToList(); 

    GridView gv = new GridView(); 
    gv.DataSource = model; 
    gv.DataBind(); 
    Session["Organizations"] = gv; 

    return View(model); 
} 

的觀點僅僅是一個正常的:

@model IEnumerable<VAGTC.Models.Organization> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Download File", "Download") 
</p> 
<table> 
    <tr> 
     <th> 
      Organization ID 
     </th> 
     <th> 
      Organization Name 
     </th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @item.OrganizationID 
     </td> 
     <td> 
      @item.Name 
     </td> 
    </tr> 
} 

</table> 

當點擊下載鏈接,它調用調用一些JavaScript的的ActionResult:

public ActionResult Download() 
{ 
    if (Session["Organizations"] != null) 
    { 
     return new DownloadFileActionResult((GridView)Session["Organizations"], "Organizations.xls"); 
    } 
    else 
    { 
     return new JavaScriptResult(); 
    } 
} 

簡單!但是,我無法弄清楚如何使用LINQ to SQL查詢。我需要能夠引用多個表。例如,將特定縣和州(組織地址)中的組織的所有地址與組織名稱(組織)一起提交。我想我必須使用ViewModel,但是我仍然無法填充它。

組織模式:

namespace VAGTC.Models 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    public partial class Organization 
    { 
     public Organization() 
     { 
      this.ContactMTMOrganizations = new HashSet<ContactMTMOrganization>(); 
      this.ContactTitles = new HashSet<ContactTitle>(); 
      this.OrganizationAddresses = new HashSet<OrganizationAddress>(); 
      this.OrganizationBusinessTypes = new HashSet<OrganizationBusinessType>(); 
      this.OrganizationCountries = new HashSet<OrganizationCountry>(); 
      this.OrganizationEmails = new HashSet<OrganizationEmail>(); 
      this.OrganizationIndustryCodes = new HashSet<OrganizationIndustryCode>(); 
      this.OrganizationMemberships = new HashSet<OrganizationMembership>(); 
      this.OrganizationNotes = new HashSet<OrganizationNote>(); 
      this.OrganizationPhones = new HashSet<OrganizationPhone>(); 
      this.OrganizationWebsites = new HashSet<OrganizationWebsite>(); 
     } 

     [Display(Name = "Organization ID:")] 
     public int OrganizationID { get; set; } 
     [Display(Name = "Organization Name:")] 
     public string Name { get; set; } 

     public virtual ICollection<ContactMTMOrganization> ContactMTMOrganizations { get; set; } 
     public virtual ICollection<ContactTitle> ContactTitles { get; set; } 
     public virtual ICollection<OrganizationAddress> OrganizationAddresses { get; set; } 
     public virtual ICollection<OrganizationBusinessType> OrganizationBusinessTypes { get; set; } 
     public virtual ICollection<OrganizationCountry> OrganizationCountries { get; set; } 
     public virtual ICollection<OrganizationEmail> OrganizationEmails { get; set; } 
     public virtual ICollection<OrganizationIndustryCode> OrganizationIndustryCodes { get; set; } 
     public virtual ICollection<OrganizationMembership> OrganizationMemberships { get; set; } 
     public virtual ICollection<OrganizationNote> OrganizationNotes { get; set; } 
     public virtual ICollection<OrganizationPhone> OrganizationPhones { get; set; } 
     public virtual ICollection<OrganizationWebsite> OrganizationWebsites { get; set; } 
    } 
} 

OrganizationAddress型號:

namespace VAGTC.Models 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 

    public partial class OrganizationAddress 
    { 
     [Display(Name = "Street:")] 
     public string StreetID { get; set; } 
     [Display(Name = "City:")] 
     public string CityID { get; set; } 
     [Display(Name = "Organization ID:")] 
     public int OrganizationID { get; set; } 
     [Display(Name = "Country:")] 
     public string CountryNameID { get; set; } 
     [Display(Name = "County:")] 
     public string CountyNameID { get; set; } 
     [Display(Name = "County State:")] 
     public string CountyStateID { get; set; } 
     [Display(Name = "State:")] 
     public string State { get; set; } 
     [Display(Name = "Zip-code:")] 
     public string ZipCode { get; set; } 
     [Display(Name = "Address Type:")] 
     public string Type { get; set; } 

     public virtual Country Country { get; set; } 
     public virtual County County { get; set; } 
     public virtual Organization Organization { get; set; } 

     public string FullAddress 
     { 
      get 
      { 
       return StreetID + ", " + CityID + ", " + State + " " + ZipCode + ", " + CountryNameID; 
      } 
     } 
     public string FullCounty 
     { 
      get 
      { 
       return CountyNameID + ", " + CountyStateID; 
      } 
     } 

    } 
} 

我將高度讚賞在正確的方向踢!

非常感謝!

編輯*

我認爲它歸結爲是能夠像填充一個列表:

List<Organization> model = db.Organizations.ToList(); 

但是從多個表。仍然在使用ViewModels,但由於我無法弄清楚如何使用Linq to SQL來填充ViewModel,所以它變得空白。

+1

你可能也想看看這個:http://doddlereport.codeplex.com/ –

+0

我當然會!非常感謝你 – cfisher

回答

1

本質上,您需要做相當於您的實體之間的SQL連接。如果啓用了延遲加載,則可以自動訪問與主要組織實體相關的所有實體。如果不是,則可以使用預先加載來訪問相同的數據(例如使用db.Organizations.Include(o => o.OrganizationAddress).ToList())。

或者您可以使用Linq to Entities Join方法。如果您想使用ViewModel,您可能需要創建一個可以存儲您感興趣的組織和相關實體的屬性的類。您只需從您的實體對象中填充它的屬性(對於本例中的組織集合中的每個項目)。

+0

謝謝 - 今晚我會試試這個! – cfisher

+0

第二種方案奏效!非常感謝你 – cfisher