Im working on a project that serves as a contact book where users can add edit delete contacts that have a few details such as email,first name, last name, phone number etc. Im trying to make it possible for a user to add multiple emails to a contact.
How would i go about doing this? At the moment im using one contact model linked with a sql database table whit all the contact variables. Would i have to create another table for emails and pair it with the contact table?
model
namespace test1.Models
{
using System;
using System.Collections.Generic;
public partial class Contact
{
public int id { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string email { get; set; }
public Nullable<int> phonenr { get; set; }
public string address { get; set; }
}
}
Controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using test1.Models;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
namespace test1.Controllers
{
[Authorize]
public class ContactController : Controller
{
private ContactBookEntities1 db = new ContactBookEntities1();
// GET: Contact
public ActionResult Index()
{
return View(db.Contacts.ToList());
}
[HttpGet]
public ActionResult Index(string Empsearch)
{
ViewData["Getcontactdetails"] = Empsearch;
var conquery = from x in db.Contacts select x;
if (!string.IsNullOrEmpty(Empsearch))
{
conquery = conquery.Where(x => x.fname.Contains(Empsearch) || x.email.Contains(Empsearch));
}
return View(conquery.AsNoTracking().ToList());
}
public FileContentResult DownloadCSV()
{
string csv = "\"ID\",\"First Name\",\"Last Name\",\"Email\",\"Phone Number\",\"Adress\" \n";
List<Contact> List = db.Contacts.ToList(); //get this list from database
foreach (Contact item in List)
{
csv = csv + String.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\" \n",
item.id,
item.fname,
item.lname,
item.email,
item.phonenr,
item.address);
}
return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "Report123.csv");
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
string filePath = string.Empty;
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
//Create a DataTable.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] {
new DataColumn("ID", typeof(int)),
new DataColumn("First Name", typeof(string)),
new DataColumn("Last Name", typeof(string)),
new DataColumn("Email", typeof(string)),
new DataColumn("Phone Number", typeof(int)),
new DataColumn("Adress",typeof(string)) });
//Read the contents of CSV file.
string csvData = System.IO.File.ReadAllText(filePath);
//Execute a loop over the rows.
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
Contact contact = new Contact();
//Execute a loop over the columns.
var cell = row.Split(',');
contact.fname = cell[1];
contact.lname = cell[2];
contact.email= cell[3];
contact.phonenr =Convert.ToInt32(cell[4]);
contact.address= cell[5];
db.Contacts.Add(contact);
db.SaveChanges();
//foreach (string cell in row.Split(','))
//{
// //dt.Rows[dt.Rows.Count - 1][i] = cell;
// i++;
//}
}
}
}
var model = db.Contacts.ToList();
return View(model);
}
[Authorize(Roles = "guest, superadmin")]
// GET: Contact/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Contact contact = db.Contacts.Find(id);
if (contact == null)
{
return HttpNotFound();
}
return View(contact);
}
// GET: Contact/Create
public ActionResult Create()
{
return View();
}
// POST: Contact/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,fname,lname,email,phonenr,address")] Contact contact)
{
//HttpPostedFileBase
if (ModelState.IsValid)
{
db.Contacts.Add(contact);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(contact);
}
// GET: Contact/Edit/5
[Authorize(Roles = "admin, superadmin")]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Contact contact = db.Contacts.Find(id);
if (contact == null)
{
return HttpNotFound();
}
return View(contact);
}
// POST: Contact/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,fname,lname,email,phonenr,address")] Contact contact)
{
if (ModelState.IsValid)
{
db.Entry(contact).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(contact);
}
// GET: Contact/Delete/5
[Authorize(Roles = "admin, superadmin")]
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Contact contact = db.Contacts.Find(id);
if (contact == null)
{
return HttpNotFound();
}
return View(contact);
}
// POST: Contact/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Contact contact = db.Contacts.Find(id);
db.Contacts.Remove(contact);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Create Contact view