Mark Tabor

Mark Tabor

  • 570
  • 1.9k
  • 433.1k

Saving Multiple Selected dropdownlist values into Db

Feb 10 2020 1:05 AM
Hi I just want to ask someone who can assists me completely , below is the detail of the problem i am getting struck with. 
I have web application in asp.net mvc4, in which i have students, courses and i am going to assign courses to students below are the table structure , i am omitting the rest of the columns and just showing the base columns for understanding purposes.
 
Student TAble: 
 Student_Id     Name
 1  ASAD
 2  TALHA
Course Table:
 Course_Id  Name
 1        MATH
 2  PHYSICS
StudentVsCourses TAble:
 Table_id  student_Id  Course_ID
 1  1  2
 2  1  1
 3  2  1
 4  2  2
 
 
Below is my view in which  i have multi select dropdown list to select multiple course and multiple students.Like if i select two students and 4 courses then it need to insert 8 rows into the database like assign four courses to student A and four courses to student B
 I omit the other columns like program,Year,Module as i would apply the same logic for them if i get success with courses and student.
below is my model class in which i am taking student & courses properties as array , and when i post these values my controller is getting the selected values as arrays , but i am confuse about how to save them i need complete code of saving the courses for each students like loop through students first and then assign all selected courses , then get back to student loop and do the same assigned for the next selected student .
 
Model class:
[Table("Student_courses")]
public class Student_Assigned_courses
{
[Key]
[Display(Name = " ID")]
public int Id { get; set; }
[Display(Name = "student Name ")]
public int[] Student_id { get; set; }
[Display(Name = "Course ")]
[Required(ErrorMessage = "course is Required")]
public int[] Course_Id { get; set; }
[Display(Name = "Module Name")]
[Display(Name = "Program Name")]
[Required(ErrorMessage = "Program selection is required field!")]
public int Program_Id { get; set; }
[Display(Name = "Year")]
[Required(ErrorMessage = "Year selection is required field!")]
public int Year_Id { get; set; }
}
}
Controller Code :
[HttpPost]
// [ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Year_Id,Program_Id,Student_id,Module_Id,Course_Id")]Student_Assigned_courses SAC,FormCollection frmcollection)
{
ViewBag.PopulatePrograms = _IEducation.PopulatePrograms();
if (SAC.Student_id != null)
{
foreach(var student in SAC.Student_id)
{
foreach(var course in SAC.Course_Id)
{
Student_Assigned_courses Sa = new Student_Assigned_courses();
for(int i=0;i
{
int s = SAC.Student_id[i];
string[] selitems = frmcollection.GetValues("Student_id");
Sa.Student_id[i] = Convert.ToInt32(SAC.Student_id[i].ToString());// this line gives error as object reference is not set to an instance of object although the  i is initialized in the loop above .
Sa.Course_Id = SAC.Course_Id;
Sa.Module_Id = SAC.Module_Id;
Sa.Year_Id = SAC.Year_Id;
Sa.Program_Id = SAC.Program_Id;
db.StudentCoursesAssigned.Add(Sa);
}
if (db.SaveChanges() > 0)
{
TempData["MessageDesignation"] = "Department Saved Successfully!";
}
}
}
return RedirectToAction("Index");
}
return View(SAC);
}

Answers (16)