Click here to Skip to main content
14,865,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I am struggling to save a value list to my database column, the issue when i try to access the property value i cant seem to get it unlike i access other property i can access without any issue. The web application is using multiple-model, below i will try to share my logic to the team and advice me where i am missing the point, the other value does save it and when i debug i can see that value without any issue.

What I have tried:

public ActionResult SaveCourse(RegCoursesViewModel eCourses, RegCoursesViewModel regCoursesView)
 {
     try
     {

         var courseName = regCoursesView.Dashboard.CourseName;
         var courseLicence = regCoursesView.eCourses.CourseLicence;
         cb.Courses.Add(regCoursesView.Courses);

         cb.SaveChanges();
     }
     catch (Exception ex)
     {
         var error = ex.Message.ToString();
     }
     return RedirectToAction("AllCourses");
 }


// eNtsaRegCourses.
  public class RegCoursesViewModel
  {
      public eNtsaCourses Courses { get; set; }
      public eNtsaCourseList eCourses { get; set; }
      public eNtsaDashboardViewModel Dashboard { get; set; }
      public IEnumerable<SelectListItem> eNtsaDashboard { get; set; }
      public RegCoursesViewModel MainModel { get; set; }

  }

<div class="form-group row">
                                                        <label for="Content-Licence" class="col-sm-3 col-form-label">Content Licence</label>
                                                        <div class="col-sm-5">
                                                            @Html.DropDownListFor(model => model.eCourses.CourseList, CourseListData as List<SelectListItem>)
                                                        </div>
                                                    </div>

public class eNtsaCourseList
 {
     public string CourseLicence { get; set; }
     public string Course_Name { get; set; }
     public int Course_Id { get; set; }
     public string CourseList { get; set; }
 }
Posted
Updated 13-Sep-20 21:04pm
Comments
Gerry Schmitz 11-Sep-20 14:52pm
   
Do you know what "SelectListItem" is? Doesn't seem to match your naming.
gcogco10 14-Sep-20 3:03am
   
Gerry yes thanks for mentioning that to me, had a work around and its working now.

1 solution

I had a work around here and did some debug first to find out each property value and i did this below solution.

// Controller
public ActionResult SaveCourse(RegCoursesViewModel MainModel)
   {
       try
       {
           MainModel.Courses = new eNtsaCourses();
           MainModel.Courses.Id = Guid.NewGuid();
           MainModel.Courses.Course = MainModel.Dashboard.CourseName;
           MainModel.Courses.CourseLicence = MainModel.eCourses.CourseLicence;
           cb.Courses.Add(MainModel.Courses);
           cb.SaveChanges();
       }
       catch (Exception ex)
       {
           var error = ex.Message.ToString();
       }
       return RedirectToAction("AllCourses");
   }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900