asp.net core 3.1 模拟数据库,假数据步骤

 

 

 

 

 

1. Model

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Webgentle.BookStore.Models
{
    public class BookModel
    {
        public int Id { get; set; }
        public string Title { get; set; }

        public string Author { get; set; }

        public string Description { get; set; }

        public string Category { get; set; }

        public string Language { get; set; }

        public int TotalPages { get; set; }

    }
}

 

 

 

 

 

 

 

2. Controller

 

 

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Webgentle.BookStore.Models;
using Webgentle.BookStore.Repositoy;

namespace Webgentle.BookStore.Controllers
{
    public class BookController : Controller
    {
        //public IActionResult Index()
        //{
        //    return View();
        //}
        private readonly BookRepository _bookRepository = null;
        public BookController() 
        {
            _bookRepository= new BookRepository();
        }
        public ViewResult GetAllBook() 
        {
            var data= _bookRepository.GetAllBooks();
            return View(data);
        }
        public ViewResult GetBooks(int id) 
        {
            var data= _bookRepository.GetBookById(id);
            return View(data);
        }

        public List<BookModel> SearchBooks(string bookName,string authorName) 
        {
            return _bookRepository.SearchBook(bookName,authorName);
        }
    }
}

 

 

3. View

 

 

@using Webgentle.BookStore.Models
@model IEnumerable<BookModel>

@{
    Layout = "_Layout";
}

<div class="container">
    <h1>all book list</h1>
    <div class="row">
        @foreach (var book in Model)
        {
            <div class="col-4">
                <div class="card shadow-sm">
                    <svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c" /><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
                    <div class="card-body">
                        <h3 class="card-title">@book.Title</h3>
                        <p class="card-text">@book.Description</p>
                        <div class="d-flex justify-content-between align-items-center">
                            <div class="btn-group">
                                <a href="/book/getbooks/@book.Id" class="btn btn-sm btn-outline-secondary">View</a>
                            </div>
                            <small class="text-muted">@book.Author</small>
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

 

 

 

 

假数据

 

 

 

using System.Collections.Generic;
using System.Linq;
using Webgentle.BookStore.Models;

namespace Webgentle.BookStore.Repositoy
{
    public class BookRepository
    {
        public List<BookModel> GetAllBooks()
        {
            return DataSource();
        }

        public BookModel GetBookById(int id) 
        {
            return DataSource().Where(x=>x.Id==id).FirstOrDefault();
        }

        public List<BookModel> SearchBook(string title,string authorName) 
        {
            return DataSource().Where(x=>x.Title.Contains(title) || x.Author.Contains(authorName)).ToList();
        }

        private List<BookModel> DataSource() 
        {
            return new List<BookModel>()
            {
                new BookModel() {Id=1,Title="MVC",Author="Nitish",Description="123123123",Category="Program1",Language="Ekem1",TotalPages=1231},
                new BookModel() {Id=2,Title="Fot",Author="Nitish",Description="123123123",Category="Program2",Language="Ekem2",TotalPages=1232},
                new BookModel() {Id=3,Title="C#",Author="Nitish",Description="123123123",Category="Program3",Language="Ekem3",TotalPages=1233},
                new BookModel() {Id=4,Title="Java",Author="Nitish", Description = "123123123",Category="Program4",Language="Ekem4",TotalPages=1234},
                new BookModel() {Id=5,Title="PHP",Author="Nitish", Description = "123123123",Category="Program5",Language="Ekem5",TotalPages=1235},
            };
        }

    }
}

 

posted @ 2023-03-09 17:24  漫漫长路</>  阅读(16)  评论(0编辑  收藏  举报