In the Asp.net MVC framework, we are implementing a feature that allows users to easily add multiple pieces of information, such as

I am currently developing a project that functions as a contact book allowing users to add, edit, and delete contacts with information such as email, first name, last name, phone number, etc. I am exploring the possibility of enabling users to add multiple emails to a single contact.

How should I approach this task? Currently, I have one contact model linked to a SQL database table with all the necessary contact variables. Would it be advisable to create another table specifically for emails and then associate it with the main 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());
        }

        ...

View to Create Contacts

Answer №1

It's the small adjustments that often lead to the most significant challenges. To streamline your code, consider converting the "email" property in your partial class to a list of strings. Additionally, establish a foreign key linking this person's contact information to an "Emails" table.

Please note that updating this structure will also necessitate modifying your view to accommodate multiple email displays. Instead of showing just one email as previously expected, you'll need to iterate through the list of emails.

   public partial class Contact
        {
            public int id { get; set; }
            public string fname { get; set; }
            public string lname { get; set; }
            *public List<string> email { get; set; }*
            public Nullable<int> phonenr { get; set; }
            public string address { get; set; }
        }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to Calculate Values Above/Below a Threshold in MySQL Database

I am attempting to calculate the number of rows that are either less than or greater than 100 in a MySQL table using PHPMyAdmin SELECT HAVING COUNT(`roadLength`) > 100 AS stop3 FROM `single-ecolo-dis-yes-tbl` and SELECT HAVING COUNT(`roadLength`)< ...

Fixed div banner when scrolling to top

I am looking to create a unique UI behavior where there is a responsive content followed by a section with a background image that scrolls along with the page until it reaches the top and then freezes. I have searched for similar solutions but haven't ...

What are the best practices for managing images (uploading, deleting, etc.) within the CakePHP framework?

I am in the process of developing a website where users can upload images for use. I would appreciate some feedback and suggestions on how to effectively manage temporary uploads. For instance, if a user uploads an image but then decides not to utilize it ...

The ASP.NET MVC 4.5 WebSocket server experiences disconnection issues after transferring a handful of messages

Recently delving into the world of WebSockets and ASP.NET, I've encountered a strange issue. Working on a basic ASP.NET 4.5 WebAPI application intended to function as an echo-server, my code is structured like this: using Microsoft.Web.WebSockets; // ...

Mastering hot reloading in React when using Dotnet and DockerorAchieving seamless hot reloading in

I have been working on getting hot reloading to function properly with React, Docker, and Dotnet. However, my research has shown that only static rendering is compatible with Docker. As a result, I find myself having to run the command docker -t build {Na ...

encountered net::ERR_EMPTY_RESPONSE while attempting to locate a CSS file within an AngularJS directive

Every time my webpage attempts to access the css file from a directive, I encounter a net::ERR_EMPTY_RESPONSE. I have implemented door3's on-demand css feature, which allows for lazy loading of css files only when necessary. This feature works flawle ...

Leveraging combobox for dynamic element selection in Selenium C#

It seems like I may be overlooking a fundamental aspect here. Is it feasible to pass a string in this format? My goal is to develop a versatile web scraper that allows the user to specify which element to locate. However, I am encountering an error stating ...

Issue: I'm unable to display the checkboxes in a checked state without duplicating the same checkbox more than once

Database Table Structure <?php // Defining the structure of the database tables CREATE TABLE `user_groups` ( `id` int(11) NOT NULL AUTOINCREMENT, `name` varchar(20) COLLATE utf8_spanish_ci NOT NULL, `description` varchar(150) COLLATE utf8_spanis ...

Responsive SmoothSlider

Need help fixing the responsive breadcrumbs in my slick slider. The slick-content-slider is taking a strange height and I want to ensure that every div remains on the same line. Thank you in advance! $('.slick-content-slider').slick({ slides ...

The 'change' event in AJAX does not trigger when the value 'select value is assigned from PHP

I have a form that includes fields for city, state, and country. The goal is to automatically populate the state and country fields when a city is selected by retrieving data from a MySQL database using Ajax. However, I am facing an issue where the Ajax c ...

Having trouble with job connectivity to MySQL service in self-hosted GitHub Actions setup on ECS

My self-hosted GitHub Actions in ECS have been performing exceptionally well, until I encountered an issue with service discovery. Here is the workflow file: name: Checking before merging for core-service server on: push: paths: - 'tons/ ...

Having trouble uploading a file using SendKeys on Firefox version 55.0.3?

I am encountering an error when trying to upload a file using the SendKeys() method in Firefox. The error message states "File not found:c:\filename.txt(IndexOutOfBounds)" Here is the code snippet for the file upload: public static void UploadTh ...

Steps for creating a Pure CSS Scroll-to-Top button

Looking to create a Pure CSS Scroll to top, I attempted it but struggled with making it clickable like in JavaScript. Here is the CSS code I tried: body { height: 99999999999999999999999999999999999px; position: relative; } button { color: white; ...

What is the best way to utilize mysql for storing user state in order to restrict them from taking a particular action more than once per day?

< button type="button" id="daily-reward-button">Claim</button> 1) When a user clicks this button, it should be disabled which will result in the user being banned in the MYSQL database and unable to click the button again. 2) The button shoul ...

Guide to configuring an x-axis scroll bar for a handsontable

Utilizing the JarvisWidget library to create widgets, I encountered an issue with a table exceeding the width of the widget when using the handsontable library. I attempted to add a scrollbar by setting the CSS width to 100% and adding overflow-x:scroll, b ...

Arranging Bootstrap panels in a stacked layout following the row

Currently, I am in the process of constructing a web dashboard for my application. This dashboard is designed to dynamically load various modules and place them within a Bootstrap container. These modules have varying heights and will stack vertically when ...

Selecting a child element based on its position within an element with a

Is there a way to use CSS to select the nth child from an element with a specific class? For instance, in the scenario below, how would I target the li element with the id="this"? Essentially, I am looking to pinpoint the 2nd element within the element t ...

To prevent the default alignment in case the text exceeds the input length, stop the automatic alignment

I have a query regarding an input field with a maximum length of 4 characters. The appearance is such that these 4 characters seem to be delineated by borders which are actually 3 lines displayed above the input field. When I enter the fourth character, a ...

Loading data onto a page using jQuery in ASP.NET after a postback

Currently experimenting with a jQuery plugin called jQuery Autocomplete Tokenizer. I'm facing an issue where I need to reload the values back into the textbox after the page is posted back, retaining whatever items were previously entered. Here' ...

The events are not being triggered by dynamically inserted link buttons

I am currently working on a project where I am dynamically adding a group of link buttons. However, when I created events for those link buttons, they are not triggering. Below is the code snippet that demonstrates this issue. namespace listofdirect ...