.net core MVC中級教程(二)

1、添加查詢我的信息頁面
2、進行添加頁面及其操做html

修改一個錯誤
在這裏插入圖片描述
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
這條語句寫錯了git

1、添加查詢我的信息頁面

在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>

在這裏插入圖片描述
在這裏插入圖片描述

2、進行添加頁面及其操做

在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

相關文章
相關標籤/搜索