1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
using ActionTireCo.Crm.Model.Database;
using ActionTireCo.Crm.Model.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace ActionTireCo.Crm.Controllers
{
public class ContactController : Controller
{
[HttpPost]
public ActionResult Add(Model.View.CustomerContact customerContact)
{
ActionTireCoCrmContext context = new ActionTireCoCrmContext();
Customer customer = (from c in context.Customer where c.Id == customerContact.CustomerId select c).SingleOrDefault<Customer>();
Contact contact = new Contact()
{
GenderId = 1,
FirstName = customerContact.Contact.FirstName,
MiddleName = customerContact.Contact.MiddleName,
LastName = customerContact.Contact.LastName,
DateCreated = DateTime.Now,
Active = true
};
context.Contact.Add(contact);
context.SaveChanges();
PhoneNumber phoneNumber = new PhoneNumber()
{
PhoneNumberTypeId = 1,
Value = customerContact.PhoneNumber.Value,
Extension = customerContact.PhoneNumber.Extension,
DateCreated = DateTime.Now,
Active = true
};
EmailAddress emailAddress = new EmailAddress()
{
EmailAddressTypeId = 1,
Value = customerContact.EmailAddress.Value,
DateCreated = DateTime.Now,
Active = true
};
context.PhoneNumber.Add(phoneNumber);
context.EmailAddress.Add(emailAddress);
context.SaveChanges();
context.ContactPhoneNumber.Add(
new ContactPhoneNumber()
{
ContactId = contact.Id,
PhoneNumberId = phoneNumber.Id,
DateCreated = DateTime.Now,
Active = true
}
);
context.ContactEmailAddress.Add(
new ContactEmailAddress()
{
ContactId = contact.Id,
EmailAddressId = emailAddress.Id,
DateCreated = DateTime.Now,
Active = true
}
);
context.SaveChanges();
Model.Database.CustomerContact customerContacts = new Model.Database.CustomerContact()
{
CustomerId = customer.Id,
ContactId = contact.Id,
DateCreated = DateTime.Now,
Active = true
};
context.CustomerContact.Add(customerContacts);
context.SaveChanges();
return RedirectToAction("Edit", "Customer", new { @customerId = customer.Id });
}
}
}
|