把ContosoUniversity例子过了一遍,好象还是有很多东西未能理解,决定自己随便加个功能看可以自己完成不....

从github的例子中clone下来ContosoUniversity项目,使用CodeFirst进行迁移后就可以得到一个正常运作的例子。

  增加的课程计划 StudentCoursePlan.cs

    public partial class StudentCoursePlan
{
public int Id { get; set; }
public int CourseId { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
public Course Course { set; get; }
}

  在SchoolContext.cs中加入定义

public DbSet<StudentCoursePlan> StudentCoursePlan { get; set; }

增加在列出学生列表时选择指定学生,可为其增加自己选择选修课程,并计算已获取多少学分功能

  在Student目录下的Index.cshtml文件添加一个按钮,按下后可以弹出一个可选定的课程列表

<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a> |
<a asp-action="Plan" asp-route-id="@item.ID">Plan</a>
</td>

  在Students.cs中增加对StudentCoursePlan的关联

public ICollection<StudentCoursePlan> StudentCoursePlan { get; set; }

  在StudentsController.cs中增加Plan的Get动作

        public async Task<IActionResult> Plan(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.Include(s => s.StudentCoursePlan)
.ThenInclude(e => e.Course)
.Include(e => e.Enrollments)
.ThenInclude(e => e.Course)
.AsNoTracking()
.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
PopulatePlanedCourse(student);
return View(student);
}
        private void PopulatePlanedCourse(Student student)
{
List<StudentCoursePlanShowData> StudentCourseShow = new List<StudentCoursePlanShowData>();
var selectedCourse = student.StudentCoursePlan.Select(s => s.Course);
var grade = "";
int? takedCredits = null;
int? sumCredits = null;
foreach (var course in selectedCourse)
{
if (course.Enrollments != null)
{
grade = student.Enrollments.Where(i => i.CourseID == course.CourseID).SingleOrDefault().Grade.ToString();
takedCredits = course.Credits / * ( - (int)student.Enrollments.Where(i => i.CourseID == course.CourseID).SingleOrDefault().Grade);
sumCredits = (sumCredits ?? ) + takedCredits;
}
else
{
grade = "";
takedCredits = null;
}
StudentCourseShow.Add(new StudentCoursePlanShowData
{
CourseID = course.CourseID,
Title = course.Title,
Grade = grade,
TakedCredits = takedCredits
});
}
ViewData["StudentCourseShow"] = StudentCourseShow;
ViewData["showNoData"] = StudentCourseShow.Count > ? "hide" : "alert alert-warning";
ViewData["sumCredits"] = sumCredits;
ViewData["studentID"] = student.ID;
ViewData["UnselectedCourse"] = new List<StudentCoursePlanShowData>();
}

  为了显示已选定的课程列表,在Models->SchoolViewModels目录下建立StudentCoursePlanShowData.cs

    public class StudentCoursePlanShowData
{
public int CourseID { get; set; }
public string Title { get; set; }
public string Grade { get; set; }
public int? TakedCredits { get; set; }
}

  Plan.cshtml

@model ContosoUniversity.Models.Student
@using ContosoUniversity.Models.SchoolViewModels;
@{
ViewData["Title"] = "Student Course Plan";
} <h2>The course that you ordered ... </h2> <div>
<h4>Student</h4>
<hr />
<p>Full Name : @Html.DisplayFor(model => model.FullName)</p>
<span class="badge badge-info">Course List</span>
<div class="@ViewData["showNoData"]">--没有数据--</div>
<input type="hidden" value=@Html.DisplayFor(model => model.ID)>
<table class="table table-bordered">
@if (((List<StudentCoursePlanShowData>)ViewData["StudentCourseShow"]).Count > )
{
<thead class="thead-inverse">
<tr>
<th>CourseID</th>
<th>Title</th>
<th>Grade</th>
<th>TakedCredits</th>
</tr>
</thead>
}
@foreach (var item in (List<StudentCoursePlanShowData>)ViewData["StudentCourseShow"])
{
<tr>
<td>
@Html.DisplayFor(ModeItem => item.CourseID)
</td>
<td>
@Html.DisplayFor(ModelItem => item.Title)
</td>
<td>
@Html.DisplayFor(ModelItem => item.Grade)
<div> </div>
</td>
<td>
@Html.DisplayFor(ModelItem => item.TakedCredits)
</td>
</tr>
}
</table>
@if (((List<StudentCoursePlanShowData>)ViewData["StudentCourseShow"]).Count > )
{
<div class="alert alert-success" role="alert">
<strong>Total TookCredts:</strong>@ViewData["sumCredits"]
</div>
} <div class="nav-divider">
<input class="btn btn-primary" type="button" id="btnPost" value="Post Test" data-toggle="modal" data-target="#modalSelectCourse" />
<a style="margin-left:20px" asp-action="Index">Back to List</a> @Html.AntiForgeryToken()
@section Scripts{
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script type="text/javascript" language="JavaScript">
$(document).ready(function () {
$("#btnPost").on('click', function () {
$("#dvShowData").load('@Url.Action("GetUnselectedCourse", "Students", new { id = Model.ID })');
});
});
</script>
}
</div>
<!-- Modal -->
<div class="modal" id="modalSelectCourse" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLabel">Course List</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form asp-action="Plan">
<div class="modal-body">
<div id="dvShowData">
@{
await Html.PartialAsync("resultsUnselectedCourse", Model);
}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div> </div>

  加粗部分的js语句调用了GetUnselectedCourse方法生成未经选定的课程列表,按下Post Test按钮时弹出部分视图resultsUnselectedCourse.cshtml来显示,部分视图代码如下:

@model ContosoUniversity.Models.Student;
@using ContosoUniversity.Models.SchoolViewModels;
<h2>FullName:@Model.FullName</h2>
<table class="table">
<thead>
<tr>
<th>CourseID</th>
<th>Title</th>
<th>Credits</th>
<th>Select</th>
</tr>
</thead>
<tbody>
@foreach (var course in ((List<StudentCoursePlanShowData>)ViewData["UnselectedCourse"]))
{
<tr>
<td>@course.CourseID</td>
<td>@course.Title</td>
<td>@course.TakedCredits</td>
<td>
<input class="checkbox" type="checkbox" name="stringSelectedCourse" value="@course.CourseID" />
</td>
</tr>
}
</tbody>
</table>

  列出未经选定课程的方法

        public async Task<IActionResult> GetUnselectedCourse(int? id)
{
var allCourses = _context.Courses;
var viewModel = new List<StudentCoursePlanShowData>();
var student = await _context.Students.Include(s => s.StudentCoursePlan).AsNoTracking().SingleOrDefaultAsync(m => m.ID == id); if (student != null)
{
var studentSelectedCourse = new HashSet<int>(student.StudentCoursePlan.Select(c => c.CourseId)); foreach (var course in allCourses)
{
if (!studentSelectedCourse.Contains(course.CourseID))
viewModel.Add(new StudentCoursePlanShowData
{
CourseID = course.CourseID,
Title = course.Title,
TakedCredits = course.Credits
});
}
}
ViewData["UnselectedCourse"] = viewModel;
return PartialView("resultsUnselectedCourse", student);
}

  选定了课程按下 Save Change 按钮时 激发 Plan 的Post 方法,记录到StudentCoursePlan表

        [HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Plan(int? id, string[] stringSelectedCourse)
{
if (id == null)
{ return NotFound(); }
if (ModelState.IsValid)
{
UpdatePlanCourse(stringSelectedCourse, id ?? );
try
{ await _context.SaveChangesAsync();
return RedirectToAction(nameof(Plan));
}
catch (DbUpdateException)
{
ModelState.AddModelError("", "更新资料失败");
}
}
var student = await _context.Students
.Include(s => s.StudentCoursePlan)
.ThenInclude(e => e.Course)
.Include(e => e.Enrollments)
.ThenInclude(e => e.Course)
.AsNoTracking()
.SingleOrDefaultAsync(m => m.ID == id);
PopulatePlanedCourse(student);
return View(student);
} private void UpdatePlanCourse(string[] stringSelectedCourse, int studentID)
{
int j;
foreach (var course in stringSelectedCourse)
{
if (!int.TryParse(course, out j))
j = ;
_context.Add(new StudentCoursePlan
{
CourseId = j,
StudentId = studentID
});
}
}

嗯,到这里就完成了,下一步看怎么完善一下它吧!

为微软ContosoUniversity例子加上学生自选课程计划的更多相关文章

  1. Imagine Cup 微软“创新杯”全球学生科技大赛

    一. 介绍 微软创新杯微博:http://blog.sina.com.cn/u/1733906825 官方站点:https://www.microsoft.com/china/msdn/student ...

  2. 【每日Scrum】第一天(4.11) TD学生助手Sprint1计划会议成果

    [每日Scrum]第一天  TD学生助手Sprint1计划会议成果 ——小组成员:刘铸辉 刘静 何晓楠 谢凤娇 胡宝月 王洪叶 初次尝试敏捷开发Scrum计划流程开发项目,有什么不对的地方还希望各位大 ...

  3. SQLSERVER教师学生成绩课程四表联合各种SQL考题

    --CREATE DATABASE EXAM_1 --GO USE EXAM_1 --判断并删除表 IF OBJECT_ID('Scores') IS NOT NULL DROP TABLE Scor ...

  4. mybatis多对多映射【学生与课程】

    1)如图 2)创建students.sql和courses.sql和middles.sql drop table middles; drop table students; drop table co ...

  5. Obtaining Directory Change Notifications(微软的例子,使用FindFirstChangeNotification,FindNextChangeNotification,FindCloseChangeNotification API函数)

    An application can monitor the contents of a directory and its subdirectories by using change notifi ...

  6. 课程计划安排 ver: 2016-12-14

    录的越多,后续肯定会涨价. <x86 从实模式到保护模式> 这本书涉及到除了汇编语言,还有一些计算机架构和操作系统方面相关的知识点. 不仅为学习高级编程语言打下了非常扎实的基础,学完C++ ...

  7. 福大软工1816 · 课程计划预报(K班)

    实践课安排 对应教学周序 时间 内容 3 09.22 业界交流讲座 6 10.13 团队选题报告答辩 7 10.20 UML设计 8 10.27 团队项目需求答辩 11 11.17 团队现场编程实战与 ...

  8. 【Deep Learning Nanodegree Foundation笔记】第 0 课:课程计划

    第一周 机器学习的类型,以及何时使用机器学习 我们将首先简单介绍线性回归和机器学习.这将让你熟悉这些领域的常用术语,你需要了解的技术进展,并了解深度学习在更大的机器学习背景中的位置. 直播:线性回归 ...

  9. JPA中映射关系详细说明(一对多,多对一,一对一、多对多)、@JoinColumn、mappedBy说明

    JPA中的映射关系 jpa中维护one to one ,one to many, many to one ,many to many 四种映射关系. 在每个关系中,双方中的一方在其表中拥有连接列.那么 ...

随机推荐

  1. 如何去掉Myeclipse对JS等文件的验证

    或 MyEclipse->validation->Excluded Resource下找到不需要验证的文件或者文件夹 或 右键点击该项目-->MyEclipse-->Exclu ...

  2. Java后端发送email实现

    依赖的jar包 <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail& ...

  3. UNITY 带spriterender的对象导出为prefab时主贴图丢失的BUG

    从场景导出带有sprite的对象为prefab时贴图丢失的BUG.解决方案:对场景中每个sprite重新赋一下贴图,然后导出就好了,原因不明. 补充:这个有时候是因为贴图类型不是 2D AND UI ...

  4. (转) Linux下配置nfs并远程挂载

    nfs是网络文件系统,允许一个节点通过网络访问远程计算机的文件系统,远程文件系统可以被直接挂载到本地,文件操作和本地没有区别,如果是局域网的nfs那么io的性能也可以保证,下面就以CentOS 7.x ...

  5. Vertex and fragment programs

    [Vertex and fragment programs] When you use vertex and fragment programs (the so called "progra ...

  6. linux: 空指令(:)

    :指令 描述: 空命令,除了参数替换和重定向外不执行任何操作,总是保证退出码为0. eg1:创建文件(不需要调用其它程序,速度更快) :>/path/to/file 测试: 创建10000个不存 ...

  7. Hadoop2.2.0安装配置手册

    第一部分 Hadoop 2.2 下载 Hadoop我们从Apache官方网站直接下载最新版本Hadoop2.2.官方目前是提供了linux32位系统可执行文件,所以如果需要在64位系统上部署则需要单独 ...

  8. 手把手教你生成二维码-google.zxing

    一.目标 输入网址,生成网址的二维码 二.概况 1.效果:UI丑,但功能实现了 2.项目目录 三.用到的第三方资源 1.google的扫码包zxing 2.JQuery 四.步骤(用myEclipse ...

  9. Mysql(Navicat for Mysql)怎么添加数据库

    1.首先打开Navicat for Mysql: 2.打开后界面如下图所示,双击连接localhost_3306: 3.连接后localhost_3306变成绿色,如下图所示: 4.选中下面任意数据库 ...

  10. pcl知识

    1.pcl/io/pcd_io.h pcl/io/ply_io.h pcl::PLYReader reader; pcl::PCDWriter pcdwriter; 读取ply pcd 2.voidl ...