Retrieve Values From Dynamically Appended MVC Partial View

Please have a look at the post,  Dynamically append view with MVC Partial View, before you continue reading this post.
 
Since an employee can have one or more than one skillset, at first I create a model for employee and skillset respectively. 
 
EmployeeModel Class
  1. public class EmployeeModel  
  2.    {  
  3.        public int EmpId { getset; }  
  4.    
  5.        public string EmpName { getset; }  
  6.    
  7.        public string Designation { getset; }  
  8.    
  9.        public string Gender { getset; }   
  10.    }  
SkillSet Class
  1. public class SkillSet  
  2.    {  
  3.        public int EmpId { getset; }  
  4.    
  5.        public string Technology { getset; }  
  6.    
  7.        public int Expertise { getset; }  
  8.    
  9.        public string Remark { getset; }  
  10.    }  
The EmpId in Skillset class serves as a foreign key to link employee and his/her skillsets.
 
In the client side, the code of getting employee basic info is very straightforward:
  1. function geEmployee() {  
  2.     var emp = {   
  3.         EmpName: $("#EditorForName").val(),  
  4.         Designation: $("#EditorForDesignation").val(),  
  5.         Gender: $("#DropDownForGender").val()  
  6.     };  
  7.     return emp;  
  8. }  
 And the javascript code to get one or multiple value for skillsets:
  1. function getSkillsets() {  
  2.            skillSets = [];  
  3.    
  4.    
  5.            const EditorTechnology = document.querySelectorAll('#EditorTechnology');  
  6.            const DropDownForExpertise = document.querySelectorAll('#DropDownForExpertise');  
  7.            const EditorFoRemark = document.querySelectorAll('#EditorFoRemark');  
  8.    
  9.    
  10.    
  11.            for (var i = 0; i < EditorTechnology.length; i++) {  
  12.                if (EditorTechnology[i].value != '') {  
  13.                    skillSets.push({  
  14.                        Technology: EditorTechnology[i].value,  
  15.                        Expertise: DropDownForExpertise[i].value,  
  16.                        Remark: EditorFoRemark[i].value  
  17.                    });  
  18.                }  
  19.            }  
  20.    
  21.            return skillSets;  
  22.        }  
The document.querySelectorAll(“#id”) will get all the values from the html form element with that particular id. Since our html form element shares the same id, all those values will be stored into an array.
 
I have 3 html form elements (Technology, Expertise and Remark), so I need 3 arrays to store all of the values respectively. 
  1. const EditorTechnology = document.querySelectorAll('#EditorTechnology');  
  2. const DropDownForExpertise = document.querySelectorAll('#DropDownForExpertise');  
  3. const EditorFoRemark = document.querySelectorAll('#EditorFoRemark');  
Then, create a for loop to iterate all values inside the array. Since all arrays contain the same length, it doesn’t matter which array you choose as a for loop iteration. (Of course we better consider using ‘Technology’ array as a benchmark, because it is what skillsets are about and ‘Remark’ array can be blank.)
 
Inside the for loop, create a variable name “skillSets” and push those values to the attributes of the skillSets object respectively.
  1. skillSets.push({  
  2.                        Technology: EditorTechnology[i].value,  
  3.                        Expertise: DropDownForExpertise[i].value,  
  4.                        Remark: EditorFoRemark[i].value  
  5.                    });  
Please note that the object name skillSets and all its attribute names (Technology, Expertise, Remark) must tally with our skillSets class, otherwise we cannot pass the object with value to the controller.
 
Next, we pass 2 objects with name “emp” and “skillSets” to our controller action named "CreateEmployee". 
  1. $(document).on('click''#Btn_Create'function (e) {    
  2.      
  3.            var emp =   geEmployee();    
  4.            var skillSets = getSkillsets();    
  5.               
  6.            $.ajax({    
  7.                type: 'POST',    
  8.                data: {   emp, skillSets  },    
  9.                url: '/Employee/CreateEmployee',    
  10.                success: function () {    
  11.                   alert('Success!')    
  12.                }    
  13.            });    
  14.        });     
C# code for CreateEmployee action,
  1. public ActionResult CreateEmployee(EmployeeModel emp, List<SkillSet> skillSets)  
  2.       {  
  3.           //some function to create a unique Emp Id before storing it to database  
  4.    
  5.           //store your data into the database here  
  6.           return View("Create");  
  7.       }  
Please make sure that the parameters name of CreateEmployee also tally with the object name that passes via ajax.
 
Then we are done, we can add multiple skillsets and click submit button, we now will be able to retrieve multiple skillsets’ value.
 
Retrieve Values From Dynamically Appended MVC Partial View
 
Retrieve Values From Dynamically Appended MVC Partial View 
 
Retrieve Values From Dynamically Appended MVC Partial View 
 
As you can see, we have a List of skillSets object with count = 3
 
Retrieve Values From Dynamically Appended MVC Partial View 
 
Retrieve Values From Dynamically Appended MVC Partial View 
  
I will stop here and not complete the process of storing the data into the database. You may want to do it your own way.
 
The full source code can be downloaded via GitHub
 
Thanks for reading.