自定义模型SearchModel ------ Model-View-Control(MVC)架构(8)

上几篇博客里,作为模型的数据都是在模型的构造函数里静态添加的,然后运行程序都没任何问题,似乎一切都很完美。

SearchModel.cpp

SearchModel::SearchModel(QObject *parent) :
    QAbstractTableModel(parent)
{
    SearchItem *item1=new SearchItem(0,1,1974,36,1,1,"Tom");
    SearchItem *item2=new SearchItem(1,2,1965,37,3,10,"这是个很长很长的名字");
    SearchItem *item3=new SearchItem(2,3,1979,35,5,20,"Kitty");
    SearchItem *item4=new SearchItem(3,4,1992,38,7,30,"Alice");
    QList<SearchItem*> list1;
    list1.append(item1);
    list1.append(item2);
    list1.append(item3);
    list1.append(item4);

    SearchItem *item5=new SearchItem(4,5,1983,39,9,40,"Lily");
    SearchItem *item6=new SearchItem(5,6,1999,37,13,50,"Mary");
    SearchItem *item7=new SearchItem(6,7,1995,37,14,51,"Maggie");
    SearchItem *item8=new SearchItem(0,1,1974,36,1,1,"Tom");
    QList<SearchIte
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的学生考试成绩管理系统的实现: 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` 即可访问学生考试成绩管理系统。可以在系统中添加、编辑和删除学生信息和成绩信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值