2018-04-05 開胃學習.Net 系列 - MVC 起步 1

本文主要講的內(nèi)容是 Code First基礎上的MVC 的起步基礎操作。






關于MVC的中心:

  1. 建立一個Model Class
  2. 建立一個Scaffolding Item,生成一個MVC 5 View
  3. View 的模板有Create,Delete,Edit 等等

這次我們的工作是嘗試起步使用一個MVC 例子,使用code first:





































稍微熟悉一下 MVC的結構后,我們在Model里面新建一個item

  1. 命名為contact.cs
  2. 輸入[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 報錯,自動可以修改,會加入using System.ComponentModel.DataAnnotations.Schema;
  3. [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 這個其實是設置自動增長屬性

利用數(shù)據(jù)生成項 DatabaseGenerated,它后有三個枚舉值: Identity:自增長;None:不處理;Computed:表示這一列是計算列
注意:在EF中,如果主鍵是int類型,Code First生成數(shù)據(jù)庫的時候會自動設置該列為自增長。但如果主鍵是Guid類型,需要手動去設置
DATA Annotations 方式:[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

  1. 寫入 [Required] 這里也會報錯,也是右擊自動修改,在最前面生成了using System.ComponentModel.DataAnnotations;
  2. [Required] OR [Required(ErrorMessage = "xxx")] 只是一個 Data Annotations 方式,表示非空
  3. 寫入[DisplayName] 報錯右擊自動加入 using System.ComponentModel;
  4. 是可以創(chuàng)建自己寫的annotation
    public class MustBeTrueAttribute : ValidationAttribute
    // MustBeTrueAttribute 是自己創(chuàng)建的,而ValidationAttribute 是.Net 本來就有的Attribute class
    {
        public override bool IsValid(object value)
        {
            return value is bool && (bool)value;
        }
    }







總結
System.ComponentModel.DataAnnotations Attributes

Attribute Description
[DisplayName] 顯示名字
[Required] 標注這個屬性的字段,不能為空,確保其有數(shù)據(jù)
[MaxLength] 給字段設定最大長度

System.ComponentModel.DataAnnotations.Schema Attributes:

Attribute Description
[DatabaseGenerated] 標注了這個特性的屬性,將會映射成數(shù)據(jù)表的計算列字段,所以這個屬性將會是只讀的。它同樣可以用來映射成自動增長列






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Homework4.Models
{
    public class Contact
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        [DisplayName("First Name")]
        [MaxLength(20, ErrorMessage = "First name must be 20 characters or less.")]
        public string FristName { get; set; }

        [Required]
        [DisplayName("Last Name")]
        [MaxLength(20, ErrorMessage = "Last name must be 20 characters or less.")]
        public string LastName { get; set; }

        [Required]
        [DisplayName("Email")]
        [DataType(DataType.EmailAddress,ErrorMessage ="Email is not valid.")]
        // 這個注釋是允許 yyy@xx.cc這樣的,所以我們選擇自制
        [Email]
        // 只需要寫Email
        [MaxLength(20, ErrorMessage = "Email must be 20 characters or less.")]
        public string Email { get; set; }

        [DisplayName("Accept Terms and Conditions")]
        [MustBeTrue]
        public string AcceptTerms { get; set; }
    }


    public class MustBeTrueAttribute : ValidationAttribute
    // MustBeTrueAttribute 是自己創(chuàng)建的,而ValidationAttribute 是.Net 本來就有的Attribute class
    {
        public override bool IsValid(object value)
        {
            return value is bool && (bool)value;
        }
    }

    public class EmailAttribute : RegularExpressionAttribute
    {
        public EmailAttribute()
        
            : base(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
        { }
    }


}

該段內(nèi)容相關鏈接:
Entity Framwork CodeFirst 學習筆記二:約定和配置
7.DataAnnotations(數(shù)據(jù)注解)【Code-First 系列】




































接下來我們嘗試一下做個scaffolding,也就是俗稱的腳手架(下面有圖)

  1. scaffolding 可以利用model來生成很多不同的內(nèi)容,我們這里嘗試生成一個View
  2. 首先把Home 里面的Contact.cshtml 刪除掉
  1. 在做后面步驟之前,首先要先rebuild resolution
  1. 然后在model里add一個新的 scaffolded item
  1. 選擇 MVC 5 View
  1. Create 就是生成新的內(nèi)容
  2. Model Class 就選擇Contact
  3. ApplicationDbContext
  4. 最后還要選擇layout page
  5. 之后會生成一個 contact.cshtml (滿是紅波浪)
  6. 之前可能搞錯了,把這個contact.cshtml 放回到View 文件夾里的home文件夾里
  7. 然后 直接build solution
  1. 單純是這樣的內(nèi)容,還無法提交,原因是什么?
  2. 回到controllers文件夾中的HomeController.cs
        // 這里只有GET ACTION,沒有SUBMIT ACTION
        [HttpPost]
        public ActionResult Contact(Contact c)
        {
            // 檢查modelstate valid,如果不是true,就把用戶重返到當下的頁面,如果是true,就返回thankyouview
            if (!ModelState.IsValid)
            {
                return View(c);
            }
            return View("ThankYouView", c);
        }
  1. 在view文件夾里的home文件夾中在add 新的thank you view
@{
    ViewBag.Title = "View";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Thanks for contacting us!</h2>
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Z說我就是你的垃圾桶,你說什么,我大部分都沒有太明白 如果沒有說話,不把心理的垃圾倒出來,那就會睡不著 有時候,將...
    ilkaynhl閱讀 486評論 0 0
  • 時間來到二零零九年暑假,二娃的兒子強強在板橋鎮(zhèn)中心小學初中畢業(yè)了,以優(yōu)異的成績考入了自貢市蜀光中學。二娃心里既高興...
    話嘮的二娃閱讀 191評論 0 0
  • 因為你們不知道,在你們心里那些無所謂的話,會對別人傷害有多大?!?0后不知名碼農(nóng) “我走過的橋,比你走過的路還多”...
    極客簡訊閱讀 540評論 0 1

友情鏈接更多精彩內(nèi)容