ASP.Net MVC Model(模型+驗證)

來源:互聯網
上載者:User

標籤:

本系列目錄:ASP.NET MVC4入門到精通系列目錄匯總

模型就是處理業務,想要儲存、建立、更新、刪除的對象。

註解(通過特性實現)

  • DisplayName
  • Required
  • StringLength(20,MinimumLength=2)
  • DataType(System.ComponentModel.DataAnnotations.DataType.MultilineText)
  • RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$",ErrorMessage="郵箱格式不對!")
.Net MVC請求處理流

請求→IIS→Runtime→Controller→Action → ViewResult(:ActionResult).ExcuteResult()→ RazorView(:IView).RenderView→Response

請求都歸結到Action上,所以是Url驅動

Action跟View是松耦合

所有的頁面繼承在viewpage類

View模板顯示頁面的規則:先找對於的Controller檔案夾,再找對應的Shared檔案夾

包括資料、驗證規則、資料訪問和商務邏輯等應用程式資訊它是我們MVCWeb應用的主架構

Model調用的是我們的商務邏輯層

Model作為ViewModel使用的情況

Model是獨立的組件,不知道View和Controller的存在

MVC驗證

System.ComponentModel.DataAnnotations

定義驗證規則:

Model屬性添加內建驗證特性,介紹用的最多的其中的四個:

    [Required], [StringLength], [Range], 和 [RegularExpression]

[StringLength(10,ErrorMessage="長度限制!")]       public string LoginName { get; set; }

定義自己的定製驗證特性,然後應用它們。你可以通過繼承自System.ComponentModel.DataAnnotations命名空間中 的ValidationAttribute基類,定義完全定製的特性。

public class EmailAttribute : RegularExpressionAttribute    {        public EmailAttribute()            :base(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")        {        }    }

Model

        [Email]        public string Email { get; set; }

View

<div>@Html.LabelFor(x=>x.Email):@Html.TextBoxFor(x=>x.Email)@Html.ValidationMessageFor(x=>x.Email)</div>

 效果:

用戶端驗證

1.引入JS指令碼支援

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

2.添加語句
    @{Html.EnableClientValidation(true);}(MVC3+版本中預設開啟)

伺服器端驗證

1.控制器中使用:ModelState.IsValid 判斷是否通過驗證

DataModel驗證

MetadataTypeAttribute Class
為資料實體的partial class添加額外的中繼資料驗證資訊
使用步驟:
建立data-model partial class
建立關聯的metadata class.
將metadata class關聯到具體的資料實體類
使用的注意事項:
標籤只能打到一個類上
此標籤不能被繼承
使用樣本:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcApplication.Models
{
    public class EmailAttribute : RegularExpressionAttribute
    {
        public EmailAttribute()
            : base(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
        {
        }
    }
    [MetadataType(typeof(UserMetadata))]
    public partial class User
    {
        public string UserName { get; set; }
        public string Remark { get; set; }
        public string Age { get; set; }
        public string Pwd { get; set; }
        public string RePwd { get; set; }
        public string Email { get; set; }
    }
    //1.為實體類添加特性DisplayName
    public class UserMetadata
    {
        [DisplayName("使用者名稱")]
        [Remote("NotExitesDdata", "Home")]
        public string UserName { get; set; }
        /// <summary>
        /// 1.在實體類中為Remark屬性設定DataType特性,指定為多行文字框
        /// </summary>
        [DataType(DataType.MultilineText)]
        [DisplayName("備忘")]
        public string Remark { get; set; }
        [DisplayName("年齡")]
        [Range(1,120)]
        public int Age { get; set; }
        [PasswordPropertyText]
        [DisplayName("密碼")]
        public string Pwd { get; set; }
        [PasswordPropertyText]
        [DisplayName("重輸密碼")]
        [System.Web.Mvc.Compare("Pwd")]
        public string RePwd { get; set; }
        [Email]
        public string Email { get; set; }
    }
}

 

ASP.Net MVC Model(模型+驗證)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.