学生将从系统中选择课程。学生可以参加一门以上的课程。一门课程可以有一个以上的学生。我可以选择一节课并将其保存到数据库中。我不知道如何上一节课。你可以帮帮我吗?
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";
}
查看:
<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控制器:
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);
}
);
});
答案 0 :(得分:0)
您在
下面发布数据{ "studentId": 35, "LessonId": [ 10, 15 ] }
您的服务器端类是
public class RootObject
{
public int studentId { get; set; }
public List<int> LessonId { get; set; }
}
serverside方法将
[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";
}