ASP.NET MVC第009天_Model介紹(1)_單筆Model傳入至View_基本DataAnnotations介紹跟檢視上DisplayFor跟DisplayNameFor使用

 
Model代表應用程式於特定某領域所需要表示的Data
Model用於對應底層資料的結構長相部負責實際資料永久儲存實作機制
跟DTO (Data Transfer Object)概念有點像
換言之,也就代表應用程式在傳遞特定商業背景領域的資料結構
經常也代表資料表的結構
在當中並不負責真正的資料存取機制


在asp.net MVC中的Model創建方式
可以透過手動方式自製
或者關聯式物件產生工具(比如Entity Framework,Linq to SQL)
實體資料模型可讓開發者藉由視覺化工具生成資料庫對應的模型類別


Step1.於Models 目錄新增Class後去撰寫如下程式
一個實際的MVC Model案例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MVCWebApp1.Models
{
    public class LoginModel
    {
        [Required]
        [Display(Name ="使用者名稱")]
        public string UserName { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [Display(Name ="使用者密碼")]
        public string Password { get; set; }
        [Display(Name ="記住密碼嗎?")]
        public bool IsRememberMe { get; set; }
    }
}


首先引用的此命名空間
using System.ComponentModel.DataAnnotations;
主要用於資料的性質 annotation
比方是否為必填
[Required]
[Required(ErrorMessage="此欄位必填")]
欄位字串長限制
[StringLength(20)]
或者正規表示式
[ReqularExpression(@"\d{8}")]
自.net 3.5之後就開始支援了

MVC中資料模型一旦建立完
對應還會需要在去配置要傳送資料的Controller程式
Step2.於Controllers目錄下新增
於HomeController 預設Index Action 我們可以去引用剛創建好的Model
首先需要引入其命名空間
之後對Model 屬性賦值再傳遞到View上

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCWebApp1.Models;
namespace MVCWebApp1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            LoginModel data = new LoginModel()
            {
                UserName="朱小明",
                Password="123456",
                IsRememberMe=true
            };
            return View(data);
        }
    }
}







Step3.產生對應的View檢視畫面
在HomeController的Index動作右鍵新增檢視



預設範本Empty調整為Details 明細
模型類別下拉選擇剛創好的LoginModel
按加入後就會於專案的Views\Home目錄下新建出Index.cshtml的檔案
跟自動生成的前端程式碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@model MVCWebApp1.Models.LoginModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <h4>LoginModel</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.UserName)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.UserName)
            </dd>
    
            <dt>
                @Html.DisplayNameFor(model => model.Password)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.Password)
            </dd>
    
            <dt>
                @Html.DisplayNameFor(model => model.IsRememberMe)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.IsRememberMe)
            </dd>
    
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
</body>
</html>


這裡再開頭引用Model部分

1
2
3
4
5
@model MVCWebApp1.Models.LoginModel

@{
    Layout = null;
}

也可以藉由之前篇章介紹到的Razor語法
@一句C#陳述式
將using 命名空間額外提取出來改寫


1
2
3
4
5
6
@using MVCWebApp1.Models
@model LoginModel

@{
    Layout = null;
}


@Html.DisplayNameFor(.....)
對應讀取自[Display(Name ="......")]

@Html.DisplayFor(.....)
對應讀取自HomeController Index Action中Model property傳遞的實際參數內容






Ref:
Data Transfer Object使用心得及時機
https://www.petekcchen.com/2010/12/how-to-use-data-transfer-object.html

What is the @Html.DisplayFor syntax for?
https://stackoverflow.com/questions/6365633/what-is-the-html-displayfor-syntax-for


[asp.net MVC]查詢結果用@Html.DisplayFor或直接用強型別變數顯示
https://dotblogs.com.tw/kevinya/2013/11/11/127563


留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients