[.NET Core 4]繼續第三篇

來源:互聯網
上載者:User

標籤:

一 使用資料庫,產生未經處理資料

原文連結:http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-2_4_5-working-with-sql.html

基礎知識:ApplicationDbContext 類負責串連資料庫並將 Movie 對象和資料記錄進行映射。 Startup.cs 檔案中,資料庫上下文是在 ConfigureServices 方法中用 Dependency Injection 容器進行註冊的。

我在ApplicationDbContext.cs中對資料庫檔案進行了重載(所以並沒有使用appsettings.json 檔案中的配置)。

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        {            // 在本地開發模式下,它會從 appsettings.json 檔案中擷取連接字串。            optionsBuilder.UseSqlite("Filename=./movie.db");        }

填充資料的方法可以參考原連結,2個步驟完成

二 Controller 方法與視圖

連結:http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-2_4_6-controller-methods-views.html 

1)不希望看到 release date 欄位顯示時間並且 ReleaseDate 應該是兩個單詞。

開啟 Models/Movie.cs 檔案並添加下面高亮的程式碼:

using System.ComponentModel.DataAnnotations; //手動高亮,添加引用
public class Movie{ public int ID { get; set; } public string Title { get; set; } [Display(Name = "Release Date")] //手動高亮 [DataType(DataType.Date)] //手動高亮 public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; }}

2)

  • Anchor Tag Helper:Views/Movies/Index.cshtml 檔案中的 MVC Core Anchor Tag Helper 自動產生Anchor(建立產生HTML元素)
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |         <!--手動高亮-->
  • [Bind] 特性:防止 over-posting (過度提交,用戶端可能發送比期望還多的資料,比如只需要2個屬性但是發送了3個屬性)的一種方法。  
  • Form Tag Helper:[ValidateAntiForgeryToken] 特性用來防止偽造請求,會在視圖最終呈現檔案中加入反偽造標記和伺服器進行配對。

基架產生的程式碼使用幾個 Tag Helper 方法來簡化 HTML 標籤。

  • Label Tag Helper 用來顯示欄位名(“Title”、”ReleaseDate”、”Genre” 或者 “Price”)。
  • Input Tag Helper 用來呈現 HTML <input> 元素。
  • Validation Tag Helper 顯示關聯到屬性的錯誤資訊。

3)驗證環境

[.NET Core 4]繼續第三篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.