首页 > 其他分享 >.Net Core 3. VS2022 + Core6.0 + Razor Razor 页面

.Net Core 3. VS2022 + Core6.0 + Razor Razor 页面

时间:2023-05-10 16:47:48浏览次数:56  
标签:Razor Core Movie Title Html VS2022 context model 页面

列表页

Pages/Movies/Index.cshtml.cs

  Razor 页面派生自 PageModel。 按照约定,PageModel 派生的类称为 PageNameModel。 例如,“索引”页命名为 IndexModel。

  这里使用IndexModel的构造函数,通过依赖注入的方式,将数据上下文对象StandardCoreStudyContext添加到页面中。

  功能还是挺简单的,就是在页面被get请求时异步请求数据库中的Movie表获取电影数据并赋值给页面的集合对象 Movie。

namespace StandardCoreStudy.Pages.Movies
{
    public class IndexModel : PageModel
    {
        private readonly StandardCoreStudy.Data.StandardCoreStudyContext _context;

        public IndexModel(StandardCoreStudy.Data.StandardCoreStudyContext context)
        {
            _context = context;
        }

        public IList<Movie> Movie { get;set; } = default!;

        public async Task OnGetAsync()
        {
            if (_context.Movie != null)
            {
                Movie = await _context.Movie.ToListAsync();
            }
        }
    }
}

  

Pages/Movies/Index.cshtml

  将页面异步获取到的Movie集合,循环显示出来。

@page
@model StandardCoreStudy.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>

  介绍几个常用指令

  @page:指令将文件转换为一个 MVC 操作,这意味着它可以处理请求,@page必须是页面上的第一个 Razor 指令。

  @model:指令可以指定传递到页面上的模型类型,比如本例中 @model StandardCoreStudy.Pages.Movies.IndexModel  这样。

  @Html.DisplayNameFor:获取模型的属性名称,@Html.DisplayNameFor(model => model.Movie[0].Title) 表示 获取这个页面 对象中的Movie集合项的Title属性的名称。

  @Html.DisplayFor:获取模型的属性值,@Html.DisplayFor(modelItem => item.Title) 表示获取Title属性的值。

  需要注意的是,DisplayNameFor 是 检查 Lambda 表达式(而非求值),因此当没有数据的时候,不会model,model.Movie[0],model.Movie[0].Title这些不会因为null而报错,而DisplayFor需要求值,所以放在循环中使用。

 

新建页

  Create.cshtml.cs 

  OnGet 方法初始化页面所需的任何状态。 “创建”页没有任何要初始化的状态,因此返回 Page。  Page 方法创建用于呈现 Create.cshtml 页的 PageResult 对象。

  Movie 属性使用 [BindProperty] 特性来选择加入模型绑定。 当“创建”表单发布表单值时,ASP.NET Core 运行时将发布的值绑定到 Movie 模型。

  当页面发布表单数据时,运行 OnPostAsync 方法

namespace StandardCoreStudy.Pages.Movies
{
    public class CreateModel : PageModel
    {
        private readonly StandardCoreStudy.Data.StandardCoreStudyContext _context;

        public CreateModel(StandardCoreStudy.Data.StandardCoreStudyContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        public Movie Movie { get; set; } = default!;
        

        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task<IActionResult> OnPostAsync()
        {
          if (!ModelState.IsValid || _context.Movie == null || Movie == null)
            {
                return Page();
            }

            _context.Movie.Add(Movie);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}

  如果没有错误,则将Movie对象写入数据库,并跳转到列表页。

 

  Razor页面部分 Create.cshtml

  

@page
@model StandardCoreStudy.Pages.Movies.CreateModel

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Movie</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Movie.Title" class="control-label"></label>
                <input asp-for="Movie.Title" class="form-control" />
                <span asp-validation-for="Movie.Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Movie.ReleaseDate" class="control-label"></label>
                <input asp-for="Movie.ReleaseDate" class="form-control" />
                <span asp-validation-for="Movie.ReleaseDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Movie.Genre" class="control-label"></label>
                <input asp-for="Movie.Genre" class="form-control" />
                <span asp-validation-for="Movie.Genre" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Movie.Price" class="control-label"></label>
                <input asp-for="Movie.Price" class="form-control" />
                <span asp-validation-for="Movie.Price" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

  <form method="post"> 元素是一个表单标记帮助程序。 表单标记帮助程序会自动包含防伪令牌。

  基架引擎在模型中为每个字段(ID 除外)创建 Razor 标记,如下所示

       <div asp-validation-summary="ModelOnly" class="text-danger"></div>            
       <div class="form-group"> <label asp-for="Movie.Title" class="control-label"></label> <input asp-for="Movie.Title" class="form-control" /> <span asp-validation-for="Movie.Title" class="text-danger"></span> </div>

验证标记帮助程序(<div asp-validation-summary 和 <span asp-validation-for)显示验证错误。 

标签标记帮助程序 (<label asp-for="Movie.Title" class="control-label"></label>) 生成标签描述和 Title 属性的 [for] 特性。

输入标记帮助程序 (<input asp-for="Movie.Title" class="form-control">) 使用 DataAnnotations 属性并在客户端生成 jQuery 验证所需的 HTML 属性。

标签:Razor,Core,Movie,Title,Html,VS2022,context,model,页面
From: https://www.cnblogs.com/luyShare/p/17388401.html

相关文章

  • 学.Net Core开发 ---- 系列文章
    原文:学.NetCore开发----系列文章-jack_Meng-博客园(cnblogs.com) 目录:     置顶:ASP.NETCore新书终于上市,完成今年一个目标,赠书活动ASP.NETCore2.0:一.概述ASP.NETCore2.0:二.开发环境ASP.NETCore2.0:三.项目结构ASP.NETCore2.0......
  • net core依赖注入
    .NetCore中依赖注入有几个关键的类型,简单介绍一下:IServiceCollection:负责存储注册的服务,可以通过其扩展方法进行服务注册;ServiceDescriptor:服务注册时的信息,如服务类型、实现类型、实例类型、生命周期等;IServiceProvider:理解是常说的容器,是IServiceCollection创建出来的,用来......
  • 推荐一个.Net Core开发的Websocket群聊、私聊的开源项目
    今天给大家推荐一个使用Websocket协议实现的、高性能即时聊天组件,可用于群聊、好友聊天、游戏直播等场景。项目简介这是一个基于.NetCore开发的、简单、高性能的通讯组件,支持点对点发送、群聊、在线状态的订阅。该项目还包含群聊例子,可以用于学习。技术架构1、跨平台:基于.Ne......
  • C# .Net Core 合并PDF文件
    使用PdfSharpCorenuget包代码实现usingMicrosoft.AspNetCore.Razor.TagHelpers;usingPdfSharpCore.Pdf;usingPdfSharpCore.Pdf.IO;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace......
  • .Net Core 2. VS2022 + Core6.0 + Razor 添加模型
    这里基本是按照微软的文档示例整理的 添加数据模型在项目中新增Models文件夹,用于存放数据模型 新增movie类usingSystem.ComponentModel.DataAnnotations;namespaceStandardCoreStudy.Models{publicclassMovie{///<summary>///ID......
  • Customising claims transformation in ASP.NET Core Identity
    I’vebeentestingoutthenewversionofASP.NETIdentityandhadtheneedtoincludeadditionalclaimsinthe ClaimIdentity generatedwhenauserisauthenticated.TransformingClaimsIdentityASP.NETCoresupportsClaimsTransformationoutofthebox.J......
  • 【asp.net core】自定义模型绑定及其验证
    引言水此篇博客,依旧是来自群里的讨论,最后说到了,在方法参数自定义了一个特性,用来绑定模型,优先从Form取,如果为空,或者不存在,在从QueryString中获取并且绑定,然后闲着无聊,就水一篇博客,如果大家有什么需求或者问题,可以找我,很高兴能为你们带来帮助。IModelBinderFactory......
  • 【HMS Core】Health Kit想要查看数据是来自用户的哪个设备,如何查看?
     【问题描述1】如何查看运动健康数据是来自用户的哪个设备?【解决方案】可以通过返回的数据中携带的dataCollectorId来查询提供数据的设备信息:请求示例(以查询睡眠记录详情为例):1、查询睡眠记录并关联睡眠状态采样数据:​2、根据关联采样数据返回的dataCollectorId调用查询指......
  • 使用 Ef core 时 报错Data is Null. This method or property cannot be called on
    1.问题在使用EFcore做查询操作的时候报错"DataisNull.ThismethodorpropertycannotbecalledonNullvalues.”"2.解决这是数据库中的某个属性为空导致,即使这个属性srting类型,也需要将字段标记为可空的......
  • C# .net core 返回json 中文字符编码被转换或乱码问题
    开发环境VS2022+.NET6.0现象接口返回Json中文数据时出现乱码。例如后台返回结果:"0506133015\u56FE\u8868\u9009\u62E9.png"。解决办法以下方法任选其一即可。//方法1:在Program.cs中添加以下代码varbuilder=WebApplication.CreateBuilder(args);builder.Services.Ad......