1

我找到了这个项目/教程:

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

而且效果很好!但是,我只能让它与一个模型一起工作。所以,如果我只引入我的组织模型,它会很好用。但是,我需要通过运行 SQL 查询来填充 GridView。我在想 LINQ to SQL 会起作用,但我想不通。

这会使用我的组织模型中的数据加载 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,该 Actionresult 会调用一些 Javascript:

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; }
    }
}

组织地址模型:

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。

4

1 回答 1

1

本质上,您需要在实体之间执行相当于 SQL 连接的操作。如果您启用了延迟加载,您可以自动访问与您的主要组织实体相关的所有实体。如果没有,您可以使用预先加载来访问相同的数据(db.Organizations.Include(o => o.OrganizationAddress).ToList()例如使用)。

或者您可以使用 Linq to Entities Join 方法。如果您想使用 ViewModel,您可能需要创建一个类来存储您感兴趣的来自组织和相关实体的属性。您只需要从您的实体对象(在本例中为您的组织集合中的每个项目)填充它的属性。

于 2012-11-28T22:11:38.537 回答