溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET MVC中如何實現數據驗證

發布時間:2021-01-30 09:51:35 來源:億速云 閱讀:235 作者:小新 欄目:web開發

這篇文章給大家分享的是有關ASP.NET MVC中如何實現數據驗證的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

首先我們在M層創建一個類:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
    public class GuestResponse
    {
        //數據驗證,必填項,如果不填ErrorMessage 請輸入你的用戶名
        [Required(ErrorMessage = "請輸入你的用戶名!")]
        public string Name { get; set; }
        //同上
        [Required(ErrorMessage = "請輸入郵箱")]
        //正則表達式,判斷是否是郵箱格式
        [RegularExpression(".+\\@.+\\..+",
        ErrorMessage = "請輸入正確的郵箱格式")]
        public string Email { get; set; }
        //同上
        [Required(ErrorMessage = "請輸入你的手機號碼")]
        public string Phone { get; set; }
        public bool? WillAttend { get; set; }
    }
}

代碼中已有注釋,不多說。

下面,V層:

@model WebApplication1.Models.GuestResponse
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RsvpForm</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
        <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
        <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
        <p>
            Will you attend?
            @Html.DropDownListFor(x => x.WillAttend, new[] {
            new SelectListItem() {Text = "Yes, I'll be there",
            Value = bool.TrueString},
            new SelectListItem() {Text = "No, I can't come",
            Value = bool.FalseString}
            }, "Choose an option")
        </p>
        <input type="submit" value="Submit RSVP" />
    }
</body>
</html>

這里注意第一行,

@model WebApplication1.Models.GuestResponse

我們綁定我們寫的數據類,這樣我們才能順利創建表單。

然后是C層:

 [HttpGet]
        public ViewResult RsvpForm() {
            return View();
        }
        [HttpPost]
        public ViewResult RsvpForm(GuestResponse model)
        {
            if (ModelState.IsValid)
            {
                // TODO: Email response to the party organizer
                return View("Thanks", model);
            }
            else
            {
                // there is a validation error
                return View();
            }
        }

這里我們有兩個RsvpForm,我們在上面添加提交方式,分別是Get和Post

if (ModelState.IsValid)

是否通過數據驗證,通過返回視圖Thanks,且把表單傳過來的值傳給Thanks視圖

數據驗證不通過,返回原視圖,

這里注意V層

 @Html.ValidationSummary()

添加這句話,在數據驗證的時候,數據不通過,視圖層才能顯示我們的錯誤提示信息。

運行截圖演示:

ASP.NET MVC中如何實現數據驗證

我們只輸入了姓名,其他沒有輸入,提示錯誤信息

我們輸入一個正確的:

ASP.NET MVC中如何實現數據驗證

然后點擊,調到Thanks視圖,Thanks視圖代碼如下:

@model WebApplication1.Models.GuestResponse

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Thanks</title>
</head>
<body>
    <p> 
        <h2>Thank you, @Model.Name!</h2>
        @if (Model.WillAttend == true)
        {
            @:It's great that you're coming. The drinks are already in the fridge!
}
        else
        {
            @:Sorry to hear that you can't make it, but thanks for letting us know.
}
    </p>
</body>
</html>

這里注意,我們也要綁定我們寫的模型類:

 @Model.Name

我們輸入的姓名

Model.WillAttend == true

我們輸入的值,像傳其他,跟這樣類似。

結果截圖:

ASP.NET MVC中如何實現數據驗證

感謝各位的閱讀!關于“ASP.NET MVC中如何實現數據驗證”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女