Saturday, March 15, 2014

MVC Controller to Show Donors and Emails

Here is the controller I created that shows the donors names and emails. This assumes that you have added the ADO Data Entity to model. It, oddly, also required me to create a new donor class to store the results of the query. It was the only way it would pass the results to the View

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;


namespace CommunityAssistMVCProject.Models
{
    public class DonorController : Controller
    {
        //
        // GET: /Donor/

        Donor d;
        CommunityAssistEntities cae = new CommunityAssistEntities();

             
        public ActionResult Index()
        {
            var don = (from p in cae.Donations
                      orderby p.Person.PersonLastName
                      where p.PersonKey == p.Person.PersonKey
                      select new { p.Person.PersonLastName, p.Person.PersonFirstName, p.Person.PersonUsername }).Distinct();

            List<Donor> donors = new List<Donor>();

            foreach (var x in don)
            {
                d = new Donor();
                d.LastName = x.PersonLastName;
                d.FirstName = x.PersonFirstName;
                d.Email = x.PersonUsername;
                donors.Add(d);
            }
                      
            return View(donors);
        }

    }

    public class Donor
    {
        public string PersonKey { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Email { get; set; }

    }
}


No comments:

Post a Comment