2012-11-28 2 views
1

에 나는이 프로젝트/튜토리얼을 발견 :MVC 그리드는 엑셀

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

을 그리고 그것은 위대한 작품! 그러나 한 모델에서만 작동하도록 할 수 있습니다. 따라서 필자가 만든 조직 모델을 사용하면 잘 작동합니다. 그러나 SQL 쿼리를 실행하여 GridView를 채울 필요가 있습니다. 나는 SQL에 LINQ가 작동한다고 생각했지만 그것을 이해할 수는 없다.

이렇게하면 내 조직 모델의 데이터가있는 GridView가로드되어보기로 전송됩니다.

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> 

이 다운로드 링크를 클릭 할 때 일부 자바 스크립트를 호출하는 Actionresult를 호출 : 간단한

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

! 그러나 LINQ를 사용하여 SQL 쿼리에 LINQ를 사용하는 방법을 알 수 없습니다. 여러 테이블을 참조 할 수 있어야합니다. 예를 들어 조직 이름 (Organization)과 함께 특정 카운티 및 주 (OrganizationAddress)에있는 조직의 모든 주소를 가져옵니다. 내가 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 놀고 있지만 ViewModel Linq 함께 SQL 채우는 방법을 알아낼 수 없기 때문에 그것은 비어 있습니다.

+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