我發現這個項目/教程: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,所以它變得空白。
你可能也想看看這個:http://doddlereport.codeplex.com/ –
我當然會!非常感謝你 – cfisher