学生成绩mvc mysql_大作业练习:用Asp.net Mvc4做一个:学生考试成绩管理系统-简易版...
时间: 2023-06-17 12:07:03 浏览: 121
好的,下面是一个简单的学生考试成绩管理系统的实现:
1. 创建数据库
首先在 MySQL 中创建一个名为 `student_score` 的数据库,然后创建以下两个表:
- `students` 表,用于存储学生信息,包括学生 ID、姓名和班级。
```
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`class` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
- `scores` 表,用于存储学生考试成绩,包括学生 ID、科目和成绩。
```
CREATE TABLE `scores` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) DEFAULT NULL,
`subject` varchar(50) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_scores_students` (`student_id`),
CONSTRAINT `FK_scores_students` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 创建 ASP.NET MVC4 项目
在 Visual Studio 中创建一个 ASP.NET MVC4 项目,选择 Empty 模板。
3. 添加模型
在项目中添加两个模型类 `Student` 和 `Score`,代码如下:
```
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Class { get; set; }
}
public class Score
{
public int Id { get; set; }
public int StudentId { get; set; }
public string Subject { get; set; }
public int Score { get; set; }
}
```
4. 添加控制器
在 Controllers 文件夹中添加一个名为 `HomeController` 的控制器,其中包含以下几个动作方法:
- `Index` 方法,用于显示学生列表和成绩列表。
```
public ActionResult Index()
{
var students = db.Students.ToList();
var scores = db.Scores.ToList();
ViewBag.Students = students;
ViewBag.Scores = scores;
return View();
}
```
- `AddStudent` 方法,用于添加新的学生信息。
```
[HttpPost]
public ActionResult AddStudent(Student student)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
```
- `AddScore` 方法,用于添加新的成绩信息。
```
[HttpPost]
public ActionResult AddScore(Score score)
{
db.Scores.Add(score);
db.SaveChanges();
return RedirectToAction("Index");
}
```
5. 添加视图
在 Views 文件夹中添加一个名为 `Index.cshtml` 的视图,用于显示学生列表和成绩列表。
```
@model IEnumerable<StudentScore.Models.Student>
@{
ViewBag.Title = "学生考试成绩管理系统";
}
<h2>学生列表</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>班级</th>
</tr>
</thead>
<tbody>
@foreach (var student in ViewBag.Students)
{
<tr>
<td>@student.Id</td>
<td>@student.Name</td>
<td>@student.Class</td>
</tr>
}
</tbody>
</table>
<h2>成绩列表</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>学生ID</th>
<th>科目</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
@foreach (var score in ViewBag.Scores)
{
<tr>
<td>@score.Id</td>
<td>@score.StudentId</td>
<td>@score.Subject</td>
<td>@score.Score</td>
</tr>
}
</tbody>
</table>
<h2>添加学生</h2>
@using (Html.BeginForm("AddStudent", "Home"))
{
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Class, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Class, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Class, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="添加" class="btn btn-default" />
</div>
</div>
}
<h2>添加成绩</h2>
@using (Html.BeginForm("AddScore", "Home"))
{
<div class="form-group">
@Html.LabelFor(model => model.StudentId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.StudentId, new SelectList(ViewBag.Students, "Id", "Name"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StudentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Subject, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Subject, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Subject, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Score, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Score, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Score, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="添加" class="btn btn-default" />
</div>
</div>
}
```
6. 运行项目
在浏览器中输入 `http://localhost:port/Home/Index` 即可访问学生考试成绩管理系统。可以在系统中添加、编辑和删除学生信息和成绩信息。
阅读全文