Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:07:04+00:00 2026-06-15T06:07:04+00:00

I found this project/tutorial: http://www.codeproject.com/Articles/325103/MVC-Grid-to-Excel-file-download And it works great! But, I can only get

  • 0

I found this project/tutorial:

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

And it works great! But, I can only get it to work with one Model. So, if I am pulling in just my Organization model it works great. However, I need to fill the GridView by running SQL queries. I was thinking LINQ to SQL would work but I cannot figure it out.

This loads the GridView with data from my Organization model and sends it to the view.

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

The view is just a normal one:

@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>

When the download link is clicked it calls the Actionresult which calls some Javascript:

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

Simple! But, I cannot figure out how to use LINQ to SQL queries with it. I need to be able to reference multiple tables. For instance, pulling in all the addresses of an Organization in a specific County and State (OrganizationAddress) along with the Organization Name (Organization). I figured I would have to use a ViewModel, however I still could not fill it up.

Organization model:

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 model:

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

    }
}

I will highly appreciate a kick in the right direction!

Thank you very much!

Edit*

What I think it boils down to is being able to populate a list like:

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

but from multiple tables. Still playing around with ViewModels but it comes up blank since I cannot figure out how to populate the ViewModel with Linq to SQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:07:05+00:00Added an answer on June 15, 2026 at 6:07 am

    Essentially, you need to do the equivalent of a SQL join between your entities. If you have Lazy Loading enabled, you can access all the entities related to your main Organization entity automatically. If not, you can use eager loading to access the same data (use db.Organizations.Include(o => o.OrganizationAddress).ToList() for example).

    Or you can use the Linq to Entities Join method. If you want to use a ViewModel, you might want to create a class that can store the properties you are interested in from Organization and related entities. You just need to populate it’s properties from your entity objects (for each item in your Organizations collection in this case).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the tutorial http://www.bit-101.com/blog/?p=2115 . In this tutorial I found a project
I found this tutorial and am trying to implement it in my project http://www.androiddevblog.net/android/playing-audio-in-android
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I just add gcal to my project according to this tutorial http://www.youtube.com/watch?v=it_9H0GxRNI but 12
i am following this tutorial: http://www.vogella.de/articles/Android/article.html#first_start but when i come to step 10.7 i
I am following this tutorial: http://www.silverlighthack.com/post/2010/03/21/Using-the-Silverlight-Bing-Maps-control-on-the-Windows-Phone-7.aspx Task 5 indicates that if I add System.Windows.Browser
I've found this tutorial: http://www.arcsynthesis.org/gltut/index.html The tutorial depends on the Unofficial OpenGL SDK (http://glsdk.sourceforge.net/docs/html/pg_build.html).
I found this tutorial: http://blog.infrared5.com/2012/05/red5-authentication/ and tried to build it. I use netbeans and
I'm using Ubuntu 10, python 2.6.5 I'm following this tutorial: http://www.djangobook.com/en/2.0/chapter02 I followed all
I am following the tutorial i found in this page http://javahunter.wordpress.com/2010/09/25/integrating-captcha-in-jsf-2-0/ to integrate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.