简体   繁体   中英

How to insert multi checkbox value into SQL Server database

Students will choose courses from the system. A student can take more than one course. One course can have more than one student. I can select one lesson and save it to the database. I don't know how to make more than one lesson. Could you help me?

Student.cs

 [Table("Student")]
 public class Student
 {
    [Key]
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual List<StudentLesson> StudentLesson { get; set; }
 }

Lesson.cs

 [Table("Lesson")]
 public class Lesson
 {
    [Key]
    public int LessonId { get; set; }
    public string LessonName { get; set; }
    public int SemesterId { get; set; }
    public  List<StudentLesson> StudentLesson { get; set; }
 }

StudentLesson.cs

public class StudentLesson
{
    [Key]
    [Column(Order =0)]
    public int StudentId { get; set; }
    [Key]
    [Column(Order =1)]
    public int LessonId { get; set; }

    public virtual Lesson Lesson { get; set; }
    public virtual Student Student { get; set; }
    public int ExamScore1 { get; set; }
    public int ExamScore2 { get; set; }
    public int AverageScore { get; set; }

MvcController.cs

  public string LessonSelect(StudentLesson stdntLess)
  {
        db.StudentLesson .Add(stdntLess);
        db.SaveChanges();
        return "lesson selected";
  }

View:

<div ng-controller="Cont">
    <div ng-repeat="(semester, lessons) in veri | groupBy:'SemesterId'">
        <h3>{{semester}}.Semester</h3>
            <div ng-repeat="lesson in lessons">
                <input type="checkbox" ng-model="lesson.selected" ng-click="select()"/>{{lesson.LessonName}} 
            </div>
    </div>
    <input type="button" class="btn-success" value="Register"  ng-click="Register()" />
</div>
</div>

AngularJs controller:

angular.module("app", [])

.controller("Cont", function ($scope, $http, $q) {

    $http.get('http://localhost:54036/***/***').then(function (response) { 
        $scope.veri = response.data;
        console.log($scope.veri2);
    }).catch(function onError(response) {
        console.log(response);
        });

    $scope.select= function ()
    {
        $scope.lessonArray=[];
        angular.forEach($scope.veri, function (lesson) {
            if (lesson.selected)
                $scope.lessonArray.push(lesson.LessonId);

        })

    }

    $scope.Register = function ()
    {
        $scope.lesson= {};
        $scope.lesson.StudentId = 35
        $scope.lesson.LessonId=$scope.lessonArray

        $http({
            method: "post",
            url: "http://localhost:54036/**/**",
            datatype: "json",
            data: JSON.stringify($scope.lesson)
        }).then(function (response) {
            alert(response.data);
        }) 
    }
})

.filter('groupBy', function () {
    return _.memoize(function (items, field) {
        return _.groupBy(items, field);
    }
    );
});

for you posting data below

{ "studentId": 35, "LessonId": [ 10, 15 ] } 

your serverside class is

public class RootObject
{
    public int studentId { get; set; }
    public List<int> LessonId { get; set; }
}

serverside method will be

[HttpPost]  
public string LessonsSelect([FromBody]RootObject data)
    {
     foreach(var lessid in data.LessonId )
       {
       var stdntLess = new StudentLesson ();
       stdntLess.StudentID = data.StudentId;
       stdntLess.LessonId = lessid;
       db.StudentLesson .Add(stdntLess);
       db.SaveChanges();
    }
      return "lesson selected";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM