【发布时间】:2017-12-01 16:53:18
【问题描述】:
学生将从系统中选择课程。一个学生可以选修一门以上的课程。一门课程可以有多个学生。我可以选择一堂课并将其保存到数据库中。我不知道怎么做不止一节课。你可以帮帮我吗?
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);
}
);
});
【问题讨论】:
-
嗨,你能告诉我 JSON.stringify($scope.lesson) 的值吗……我可以为你创建方法……在控制台上写下来告诉我
-
json:{"StudentId":35,"LessonId":[6]}
-
嗨,请检查我的答案,提供您的代码,这是针对 webapi,但对于 asp.net mvc(在 asp.net mvc 上的 exp 较少)大部分情况相同,因此在我使用的 Web api 中提供了代码对于角度 js
-
更新的代码请检查它是否在你的最后工作
-
你必须在集合上做 foreach ....更新了我的答案
标签: c# angularjs sql-server asp.net-mvc checkboxlist