2 Replies - 806 Views - Last Post: 14 September 2015 - 07:51 PM Rate Topic: -----

#1 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Using a Submit button to add to an array.

Posted 11 September 2015 - 08:46 AM

I am still a student and have very little experience managing a database (close to none). So what I am stuck on trying to figure out is I have "followers" who select Menu Items from a Menu on a restaurants page (using check boxes and a submit button). I have the model configured to where the followers have an array of MenuItems in their table, but I am having trouble with getting the coding right for it to work where the submit button adds those menu items to the followers array.

Here is the action method I have so far:

 public ActionResult AddMenuItems(int MenuItemID)
        {
        //  Need to run through array of MEnuItem ID's and add them to the Followers LIst of MEnuItems
       
            
            foreach (var menuItem in Model.MenuItems)//Getting an error here because it can't call Model.MenuItems, not sure how to properly get there....
            {
                Model.MenuItems menuItem = new Model.MenuItems();  
                menuItem.menuItemID = menuItem.Key;
                if (menuItem.key == MenuItemID) 
                { 
                    Follower.Add(menuItem); 
                }
                   
            }
        
        }


Am I heading in the right direction? Can anyone post pseudocode to lead me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Using a Submit button to add to an array.

#2 zerocool1   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 62
  • Joined: 31-August 15

Re: Using a Submit button to add to an array.

Posted 14 September 2015 - 07:10 PM

I'm not sure this will hit exactly what you're looking for because I'm not 100% positive. But basically it seems you want to get the menu items selected and add them do a database. I won't code the entire thing, I think you can figure out how to create the form and send the items to the array, so I just created a simple array as demonstrating looping through using a connection and inserting records to the DB.

            string[] menuitemarray = new string[4]; // create array of menu items

            menuitemarray[0] = "Pork"; 
            menuitemarray[1] = "Brisket"; // set array values
            menuitemarray[2] = "Chicken";
            menuitemarray[3] = "Streak";

            string connectionString = "Data Source=ServerName; Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; // create database connection

            using (SqlConnection connection = new SqlConnection(connectionString)) // create connection
            {

                
                foreach (string value in menuitemarray) //loop through each item in the array using value string
                {
                    connection.Open();  // open connection 
                    SqlCommand command = new SqlCommand("insert into tbl_menu select @value", connection); // insert into the database based on parameter
                    command.Parameters.Add("@value", SqlDbType.VarChar).Value = value; //set parameter @ value = value string 
                    command.ExecuteNonQuery(); // execute query
                    connection.Close(); // close connection 

                }



            }


Was This Post Helpful? 1
  • +
  • -

#3 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Using a Submit button to add to an array.

Posted 14 September 2015 - 07:51 PM

Thanks for your help with this. It has been a few days since I posted this but I believe I figured it out. I am getting some errors in the rest of the program that aren't making sense to me.

But here is the code I ended up going with that I haven't tested yet:

   public ActionResult AddMenuItems(List<int> MenuItemID)
        {
        //  Need to run through array of MEnuItem ID's and add them to the Followers LIst of MEnuItems
            Follower follower = db.Followers.Find(id);
            // Remove deselected menu items
            follower.MenuItems.Where(m => !menuItemId.Contains(m.Id)).ToList()
                .ForEach(m => follower.MenuItems.Remove(m));

            // Add newly selected menu items
            var existingMenuItemIds = follower.MenuItems.Select(m => m.Id).ToList();
            db.MenuItems.Where(m => menuItemIds.Except(existingMenuItemIds).Contains(m.Id)).ToList()
                .ForEach(m => follower.MenuItems.Add(m));
        
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1