学生成绩mvc mysql_大作业练习:用Asp.net Mvc4做一个:学生考试成绩管理系统-简易版...
时间: 2024-05-07 15:21:30 浏览: 13
好的,这个系统可以包括以下功能:
1. 学生管理:添加、删除、修改、查询学生信息;
2. 课程管理:添加、删除、修改、查询课程信息;
3. 成绩管理:录入、修改、查询学生成绩;
4. 用户登录:学生、教师、管理员登录;
5. 数据库管理:备份、还原数据库。
首先,我们需要创建一个 ASP.NET MVC 4 项目,并在项目中创建以下模型类:
```c#
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Major { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Score
{
public int Id { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
public double Grade { get; set; }
}
```
接下来,我们需要创建一个数据库上下文类,并在其中定义上述模型类对应的 DbSet:
```c#
public class ScoreContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<Score> Scores { get; set; }
}
```
然后,我们需要创建一个控制器类,用于处理学生、课程、成绩的增删改查操作:
```c#
public class HomeController : Controller
{
private ScoreContext db = new ScoreContext();
// 学生管理
public ActionResult Students()
{
var students = db.Students.ToList();
return View(students);
}
public ActionResult AddStudent()
{
return View();
}
[HttpPost]
public ActionResult AddStudent(Student student)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Students");
}
public ActionResult EditStudent(int id)
{
var student = db.Students.Find(id);
return View(student);
}
[HttpPost]
public ActionResult EditStudent(Student student)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Students");
}
public ActionResult DeleteStudent(int id)
{
var student = db.Students.Find(id);
db.Students.Remove(student);
db.SaveChanges();
return RedirectToAction("Students");
}
// 课程管理
public ActionResult Courses()
{
var courses = db.Courses.ToList();
return View(courses);
}
public ActionResult AddCourse()
{
return View();
}
[HttpPost]
public ActionResult AddCourse(Course course)
{
db.Courses.Add(course);
db.SaveChanges();
return RedirectToAction("Courses");
}
public ActionResult EditCourse(int id)
{
var course = db.Courses.Find(id);
return View(course);
}
[HttpPost]
public ActionResult EditCourse(Course course)
{
db.Entry(course).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Courses");
}
public ActionResult DeleteCourse(int id)
{
var course = db.Courses.Find(id);
db.Courses.Remove(course);
db.SaveChanges();
return RedirectToAction("Courses");
}
// 成绩管理
public ActionResult Scores()
{
var scores = db.Scores.Include(s => s.Student).Include(s => s.Course).ToList();
return View(scores);
}
public ActionResult AddScore()
{
ViewBag.StudentId = new SelectList(db.Students, "Id", "Name");
ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name");
return View();
}
[HttpPost]
public ActionResult AddScore(Score score)
{
db.Scores.Add(score);
db.SaveChanges();
return RedirectToAction("Scores");
}
public ActionResult EditScore(int id)
{
var score = db.Scores.Find(id);
ViewBag.StudentId = new SelectList(db.Students, "Id", "Name", score.StudentId);
ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name", score.CourseId);
return View(score);
}
[HttpPost]
public ActionResult EditScore(Score score)
{
db.Entry(score).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Scores");
}
public ActionResult DeleteScore(int id)
{
var score = db.Scores.Find(id);
db.Scores.Remove(score);
db.SaveChanges();
return RedirectToAction("Scores");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
```
最后,我们需要创建视图文件,用于展示学生、课程、成绩的信息以及进行增删改查操作。
学生管理视图:
```html
@model List<Student>
<h2>学生管理</h2>
<p>
<a href="@Url.Action("AddStudent")">添加学生</a>
</p>
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>专业</th>
<th>操作</th>
</tr>
@foreach (var student in Model)
{
<tr>
<td>@student.Id</td>
<td>@student.Name</td>
<td>@student.Gender</td>
<td>@student.Major</td>
<td>
<a href="@Url.Action("EditStudent", new { id = student.Id })">编辑</a>
<a href="@Url.Action("DeleteStudent", new { id = student.Id })">删除</a>
</td>
</tr>
}
</table>
```
添加学生视图:
```html
@model Student
<h2>添加学生</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
<label for="Name">姓名:</label>
@Html.TextBoxFor(model => model.Name)
</div>
<div>
<label for="Gender">性别:</label>
@Html.TextBoxFor(model => model.Gender)
</div>
<div>
<label for="Major">专业:</label>
@Html.TextBoxFor(model => model.Major)
</div>
<p>
<input type="submit" value="提交" />
</p>
}
```
课程管理视图和添加课程视图类似,这里不再赘述。成绩管理视图:
```html
@model List<Score>
<h2>成绩管理</h2>
<p>
<a href="@Url.Action("AddScore")">添加成绩</a>
</p>
<table>
<tr>
<th>ID</th>
<th>学生</th>
<th>课程</th>
<th>成绩</th>
<th>操作</th>
</tr>
@foreach (var score in Model)
{
<tr>
<td>@score.Id</td>
<td>@score.Student.Name</td>
<td>@score.Course.Name</td>
<td>@score.Grade</td>
<td>
<a href="@Url.Action("EditScore", new { id = score.Id })">编辑</a>
<a href="@Url.Action("DeleteScore", new { id = score.Id })">删除</a>
</td>
</tr>
}
</table>
```
添加成绩视图:
```html
@model Score
<h2>添加成绩</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
<label for="StudentId">学生:</label>
@Html.DropDownList("StudentId", null, "-- 请选择 --")
</div>
<div>
<label for="CourseId">课程:</label>
@Html.DropDownList("CourseId", null, "-- 请选择 --")
</div>
<div>
<label for="Grade">成绩:</label>
@Html.TextBoxFor(model => model.Grade)
</div>
<p>
<input type="submit" value="提交" />
</p>
}
```
以上就是一个简单的学生考试成绩管理系统的实现,可以根据需要进行修改和扩展。