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

首先我們在M層創(chuàng)建一個類:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class GuestResponse
{
//數(shù)據(jù)驗證,必填項,如果不填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
我們綁定我們寫的數(shù)據(jù)類,這樣我們才能順利創(chuàng)建表單。
然后是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)
是否通過數(shù)據(jù)驗證,通過返回視圖Thanks,且把表單傳過來的值傳給Thanks視圖
數(shù)據(jù)驗證不通過,返回原視圖,
這里注意V層
@Html.ValidationSummary()
添加這句話,在數(shù)據(jù)驗證的時候,數(shù)據(jù)不通過,視圖層才能顯示我們的錯誤提示信息。
運行截圖演示:

我們只輸入了姓名,其他沒有輸入,提示錯誤信息
我們輸入一個正確的:

然后點擊,調(diào)到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
我們輸入的值,像傳其他,跟這樣類似。
結(jié)果截圖:

感謝各位的閱讀!關(guān)于“ASP.NET MVC中如何實現(xiàn)數(shù)據(jù)驗證”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁題目:ASP.NETMVC中如何實現(xiàn)數(shù)據(jù)驗證-創(chuàng)新互聯(lián)
文章鏈接:http://chinadenli.net/article16/ddpodg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、搜索引擎優(yōu)化、手機網(wǎng)站建設、微信小程序、網(wǎng)站策劃、網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容