1、添加查询我的信息页面
2、进行添加页面及其操做html
修改一个错误
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
这条语句写错了git
在HomeController中添加查询语句github
[HttpGet] public IActionResult Detail(int id) { var student = _repository.GetById(id); if (student == null) { return NotFound(); } return View(student); }
修改下index里面的代码,写个超连接web
@model TutorialStudy.Views.ViewModel.HomeIndexViewModel @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width"/> <title>学生信息主页</title> </head> <body> <h1>Student</h1> <ul> @foreach (var s in Model.Students) { <li> @s.Name (@s.Age) <a asp-action="Detail" asp-route-id="@s.Id">学生信息明细</a> </li> } </ul> </body> </html>
在view,home中添加Detail视图mvc
@model TutorialStudy.Model.Student <!DOCTYPE html> <html> <head> <title>@Model.FirstName@Model.LastName 信息</title> </head> <body> <h1>@Model.Id</h1> <h1>@Model.FirstName</h1> <h1>@Model.LastName</h1> <h1>@Model.Gender</h1> <h1>@Model.BirthDate</h1> <a asp-action="Index">返回主界面</a> </body> </html>
在Viewmodel中添加Create的模板StudentCreateViewModelsvg
using System; using TutorialStudy.Model; namespace TutorialStudy.Views.ViewModel { public class StudentCreateViewModel { public string LastName { get; set; } public string FirstName { get; set; } public Gender Gender { get; set; } public DateTime Birthday { get; set; } } }
而后改下IRepositorypost
using System.Collections.Generic; using TutorialStudy.Model; namespace TutorialStudy.Services { public interface IRepository<T> where T:class { IEnumerable<T> GetAll(); T GetById(int studentId); T Add(Student student); } }
建立的语句改下,等下传回来的是student类spa
using System.Collections.Generic; using TutorialStudy.Model; namespace TutorialStudy.Services { public interface IRepository<T> where T:class { IEnumerable<T> GetAll(); T GetById(int studentId); T Add(Student student); } }
在实现类InMemoryRepository中添加.net
public Student Add(Student student) { var maxId = _student.Max(x => x.Id); student.Id = ++maxId; _student.Add(student); return student; }
viewmodel添加StudentCreateViewModel类3d
using System; using TutorialStudy.Model; namespace TutorialStudy.Views.ViewModel { public class StudentCreateViewModel { public string LastName { get; set; } public string FirstName { get; set; } public Gender Gender { get; set; } public DateTime BirthDate { get; set; } } }
在HomeController添加
[HttpGet] public IActionResult Create() { return View(); } [HttpPost] public IActionResult Create(StudentCreateViewModel model) { var student=new Student { FirstName = model.FirstName, LastName = model.LastName, BirthDate = model.BirthDate, Gender = model.Gender }; _repository.Add(student); return View("Detail",student); }
接下来是添加视图的操做了
在Index中写添加的超连接
接着视图建立Create
@using TutorialStudy.Model @model TutorialStudy.Views.ViewModel.StudentCreateViewModel <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>添加学生</title> </head> <body> <form method="post"> <h1>建立一个学生</h1> <input asp-for="FirstName"/> <input asp-for="LastName"/> <input asp-for="BirthDate" type="date"/> <select asp-for="Gender" asp-items="Html.GetEnumSelectList<Gender>()"></select> <button type="submit">添加</button> </form> </body> </html>
运行
能够,有点缺陷是主界面显示不出来添加的这个
等下在解决吧,先写到这里了
github代码地址
https://github.com/1045683477/.net-core-mvc-intermediate