• 在 ASP.NET Core 中向 Razor Pages 应用显示模型


    以下是实现也一个asp.net core Razor Pages的基本步骤

    1、定义模型元素

    复制代码
    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace RazorPagesMovie.Models
    {
        public class Movie
        {
            public int ID { get; set; }
            public string Title { get; set; }
    
            [DataType(DataType.Date)]
            public DateTime ReleaseDate { get; set; }
            public string Genre { get; set; }
            public decimal Price { get; set; }
        }
    }
    复制代码

    2、创建数据库上下文

    复制代码
    using Microsoft.EntityFrameworkCore;
    
    namespace RazorPagesMovie.Models
    {
        public class RazorPagesMovieContext : DbContext
        {
            public RazorPagesMovieContext (DbContextOptions<RazorPagesMovieContext> options)
                : base(options)
            {
            }
    
            public DbSet<RazorPagesMovie.Models.Movie> Movie { get; set; }
        }
    }
    复制代码

    3、将数据库上下文添加到服务中

    复制代码
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
    
        services.AddDbContext<RazorPagesMovieContext>(options =>
          options.UseSqlServer(Configuration.GetConnectionString("RazorPagesMovieContext")));
    }
    复制代码

    4、Razor Pages页面代码管理

    复制代码
    // Unused usings removed.
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.EntityFrameworkCore;
    using RazorPagesMovie.Models;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    namespace RazorPagesMovie.Pages.Movies
    {
        public class IndexModel : PageModel
        {
            private readonly RazorPagesMovie.Data.RazorPagesMovieContext _context;
    
            public IndexModel(RazorPagesMovie.Data.RazorPagesMovieContext context)
            {
                _context = context;
            }
    
            public IList<Movie> Movie { get;set; }
    
            public async Task OnGetAsync()
            {
                Movie = await _context.Movie.ToListAsync();
            }
        }
    }
    复制代码

    5、模型和页面关联

    复制代码
    @page
    @model RazorPagesMovie.Pages.Movies.IndexModel
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <p>
        <a asp-page="Create">Create New</a>
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Movie[0].Title)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Movie[0].ReleaseDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Movie[0].Genre)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Movie[0].Price)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
    @foreach (var item in Model.Movie) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ReleaseDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Genre)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    <a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
                    <a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
                    <a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
                </td>
            </tr>
    }
        </tbody>
    </table>
    复制代码
  • 相关阅读:
    初学Python语言者必须理解的下划线
    Python初学者必须了解的星号(*)90%的人都不懂
    90%人不知道的Python炫技操作:合并字典的七种方法
    用Python爬取了妹子网100G的套图,值得收藏
    这种python反爬虫手段有点意思,看我怎么破解
    函数极限(上)
    数学分析--实数和数列极限--数轴
    B1046. 划拳
    B1026. 程序运行时间
    2019考研英语一 Text2分析
  • 原文地址:https://www.cnblogs.com/minhost/p/12153059.html
Copyright © 2020-2023  润新知