这里是C#利用ASP.NET Core开发学生管理系统的完整攻略。
步骤一:创建.NET Core Web应用程序
- 打开Visual Studio,并以管理员身份运行。
- 在Visual Studio中选择“文件”>“新建”>“项目”。
- 选择".NET Core"类别,然后选择“ASP.NET Core Web应用程序”模板(或“ASP.NET Core Web应用程序(模型-视图-控制器)”模板)
- 为项目命名,并选择合适的位置。
- 选择“Web应用程序(.NET Core)”模板,并确保在“身份验证”下选择“无身份验证”。
- 创建项目后,可以在文件夹树中看到多个默认文件,包括Controllers和Views文件夹,Startup.cs和Program.cs文件以及wwwroot静态文件夹。
步骤二:连接数据库
- 在Visual Studio的“工具”选项中打开“NuGet包管理器控制台”。
- 在“包管理器控制台”窗口中,键入以下命令来安装Microsoft.EntityFrameworkCore.Tools包:
Install-Package Microsoft.EntityFrameworkCore.Tools
- 随后,安装需要与数据库进行通信的数据库提供程序软件包,如Microsoft.EntityFrameworkCore.SqlServer。
Install-Package Microsoft.EntityFrameworkCore.SqlServer
- 在项目文件夹中创建名为“Data”的新文件夹。
- 在“Data”文件夹中创建以下上下文类:
using Microsoft.EntityFrameworkCore;
namespace StudentManagementSystem.Models{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Student> Students { get; set; }
}
}
- 打开Startup.cs文件并替换ConfigureServices()方法的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("StudentDBConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
步骤三:创建Model
- 在Models文件夹中创建一个名为Student.cs的新类。
namespace StudentManagementSystem.Models{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Gender? Gender { get; set; }
public string Email { get; set; }
public string PhotoPath { get; set; }
}
}
- 请注意,此类遵循“单个职责原则”,并且只包含与学生相关的信息。
步骤四:创建控制器和视图
- 在Controllers文件夹中创建一个名为StudentController.cs的新类。
using Microsoft.AspNetCore.Mvc;
using StudentManagementSystem.Models;
using System.Linq;
namespace StudentManagementSystem.Controllers{
public class StudentController : Controller
{
private readonly AppDbContext _context;
public StudentController(AppDbContext context)
{
_context = context;
}
public IActionResult Index()
{
var students = _context.Students.ToList();
return View(students);
}
}
}
- 创建“Views”文件夹,然后在该文件夹中创建名为“Student”的新文件夹。
- 在“Student”文件夹中创建“Index.cshtml”视图。
@model IEnumerable<StudentManagementSystem.Models.Student>
@{
ViewData["Title"] = "Index";
}
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
</tr>
</thead>
<tbody>
@foreach (var student in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => student.Name)
</td>
<td>
@Html.DisplayFor(modelItem => student.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => student.Email)
</td>
</tr>
}
</tbody>
</table>
- 启动应用程序,然后导航到“https://localhost:44355/Student/Index”以查看学生信息。
示例一:插入数据
以添加一个新学生的过程为例子:
1.在StartUp.cs里面添加头文件:
using StudentManagementSystem.ViewModels;
2.在Models文件夹里面,创建一个新文件夹ViewModels,在该文件夹下创建一个新的类StudentCreateViewModel.cs:
using System.ComponentModel.DataAnnotations;
using StudentManagementSystem.Models;
namespace StudentManagementSystem.ViewModels
{
public class StudentCreateViewModel
{
[Required(ErrorMessage = "请填写名字")]
[MaxLength(50, ErrorMessage = "名字长度不能超过50个字符")]
public string Name { get; set; }
[Required]
public Gender? Gender { get; set; }
[Required(ErrorMessage = "请填写电子邮件地址")]
[EmailAddress(ErrorMessage = "请填写正确的电子邮件地址")]
public string Email { get; set; }
public string PhotoPath { get; set; }
}
}
3.修改StudentController.cs:
public class StudentController : Controller
{
private readonly AppDbContext _context;
public StudentController(AppDbContext context)
{
_context = context;
}
public IActionResult Index()
{
var students = _context.Students.ToList();
return View(students);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(StudentCreateViewModel model)
{
if (ModelState.IsValid)
{
var student = new Student
{
Name = model.Name,
Gender = model.Gender,
Email = model.Email,
PhotoPath = model.PhotoPath
};
_context.Students.Add(student);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
}
public IActionResult Create(StudentCreateViewModel model)
{
if (ModelState.IsValid)
{
var student = new Student
{
Name = model.Name,
Gender = model.Gender,
Email = model.Email,
PhotoPath = model.PhotoPath
};
_context.Students.Add(student);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
4.修改Views/Student中的Create.cshtml:
@model StudentManagementSystem.ViewModels.StudentCreateViewModel
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gender" class="control-label"></label>
<select asp-for="Gender" class="form-control">
<option value="">---请选择性别---</option>
<option value="0">女</option>
<option value="1">男</option>
</select>
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PhotoPath" class="control-label"></label>
<input asp-for="PhotoPath" type="file" />
<span asp-validation-for="PhotoPath" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
5.访问https://localhost:44355/Student/Create,可以显示表单;在必填项中填入信息,提交表单后数据有效性检查结果正确,数据被插入。
示例二:编辑和删除数据
以编辑和删除学生信息为例:
1.在Views/Student中的Index.cshtml中添加编辑和删除按钮:
<td>
<a asp-action="Edit" asp-route-id="@student.Id">编辑</a>
<a asp-action="Delete" asp-route-id="@student.Id" class="btn btn-danger"
onclick="return confirm('Are you sure to delete?')">删除</a>
</td>
2.在StudentController.cs添加Edit()方法:
public IActionResult Edit(int id)
{
var student = _context.Students.Find(id);
if (student == null)
{
return NotFound();
}
var studentEditViewModel = new StudentEditViewModel
{
Id = student.Id,
Name = student.Name,
Gender = student.Gender,
Email = student.Email,
ExistingPhotoPath = student.PhotoPath
};
return View(studentEditViewModel);
}
3.在ViewModels文件夹中创建StudentEditViewModel.cs:
using StudentManagementSystem.Models;
namespace StudentManagementSystem.ViewModels
{
public class StudentEditViewModel : StudentCreateViewModel
{
public int Id { get; set; }
public string ExistingPhotoPath { get; set; }
}
}
4.修改Views/Student中的Edit.cshtml:
@model StudentManagementSystem.ViewModels.StudentEditViewModel
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<form asp-action="Edit">
@Html.HiddenFor(model => model.Id)
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gender" class="control-label"></label>
<select asp-for="Gender" class="form-control">
<option value="">---请选择性别---</option>
<option value="0">女</option>
<option value="1">男</option>
</select>
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<input type="hidden" asp-for="ExistingPhotoPath" />
<label asp-for="PhotoPath" class="control-label"></label>
<input asp-for="PhotoPath" class="form-control" />
<span asp-validation-for="PhotoPath" class="text-danger"></span>
</div>
<div class="form-group">
<img src="@Model.ExistingPhotoPath" alt="个人照片" style="max-height: 200px" />
</div>
<div class="form-group">
<input type="submit" value="提交" class="btn btn-primary" />
</div>
</form>
<div>
<a asp-action="Index">返回</a>
</div>
5.在StudentController.cs添加Edit()方法中添加相应的HttpPost方法:
[HttpPost]
public IActionResult Edit(StudentEditViewModel model)
{
if (ModelState.IsValid)
{
var student = _context.Students.Find(model.Id);
if (student == null)
{
return NotFound();
}
student.Name = model.Name;
student.Gender = model.Gender;
student.Email = model.Email;
student.PhotoPath = model.PhotoPath;
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
6.添加Delete()方法:
public IActionResult Delete(int id)
{
var student = _context.Students.Find(id);
if (student == null)
{
return NotFound();
}
_context.Students.Remove(student);
_context.SaveChanges();
return RedirectToAction("Index");
}
7.访问https://localhost:44355/Student/Index,可以看到每个学生条目后面都有一个编辑和删除链接。点击“编辑”链接会打开编辑页并显示学生信息。在编辑页上提交表单后,数据已被更新。点击“删除”链接将删除相应的学生信息。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#利用ASP.NET Core开发学生管理系统详解 - Python技术站