MVC數(shù)據(jù)驗(yàn)證詳解
一、一般情況
對(duì)于使用過(guò)MVC框架的人來(lái)說(shuō),對(duì)MVC的數(shù)據(jù)驗(yàn)證不會(huì)陌生,比如,我有一個(gè)Model如下:
public class UserInfo { [Required(ErrorMessage = "UserName不可為空1111")] public string UserName { get; set; } public string Sex { get; set; } public string Mobile { get; set; } public string Address { get; set; } }
前端:
@using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>UserInfo</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> }
效果:
是的,MVC可以通過(guò)對(duì)一些屬性添加一定的特性來(lái)對(duì)數(shù)據(jù)進(jìn)行驗(yàn)證。這對(duì)大家來(lái)說(shuō)可能并不陌生。
如果僅僅是這樣就完事了,那么也就沒(méi)事么意思了。
二、常用情況
在實(shí)際的開(kāi)發(fā)中,我們大都是通過(guò)EF,或者其他方式,使得數(shù)據(jù)庫(kù)中的每一個(gè)表或視圖,都在代碼中對(duì)應(yīng)的一個(gè)類(lèi)模型,對(duì)于通過(guò)數(shù)據(jù)庫(kù)生成的模型,我們不宜修改,退一步講,即使我們?cè)谶@個(gè)類(lèi)中對(duì)一些屬性增加一些數(shù)據(jù)驗(yàn)證的特性,那么,數(shù)據(jù)庫(kù)發(fā)生變化后,如果我再重新生成這些Model,我們之前添加好的驗(yàn)證特性將沒(méi)有了,那么,我們?nèi)绾谓鉀Q這樣的問(wèn)題呢?
假如:
public class UserInfo { public string UserName { get; set; } public string Sex { get; set; } public string Mobile { get; set; } public string Address { get; set; } }
UserInfo是通過(guò)數(shù)據(jù)庫(kù)生成的一個(gè)模型,對(duì)于數(shù)據(jù)庫(kù)生成的模型,我們不宜修改。但那是,我們又需要對(duì)這個(gè)模型中的某些屬性進(jìn)行數(shù)據(jù)驗(yàn)證,比如需要對(duì)UserName屬性進(jìn)行非空驗(yàn)證,那么我們?nèi)绾巫瞿兀?/p>
大家通常會(huì)想到部分類(lèi),是的,我們可以通過(guò)部分類(lèi)來(lái)解決上述問(wèn)題。
首先,我們將模型中的類(lèi)加上關(guān)鍵字 partial ,然后我們?cè)賹?xiě)一個(gè)這個(gè)模型的部分類(lèi)。
public partial class UserInfo { [Required(ErrorMessage = "UserName不可為空1111")] public string UserName { get; set; } }
但是,這樣會(huì)提示我們一個(gè)錯(cuò)誤,就是類(lèi)中存在重復(fù)的屬性,是的,部分類(lèi)中,屬性是不可以重名的。那么,我們?cè)撛趺崔k呢,MVC框架已經(jīng)給了我們解決方案了。
我們可以這么寫(xiě):
[MetadataType(typeof(MeteUserInfo))] public partial class UserInfo { private class MeteUserInfo { [Required(ErrorMessage = "UserName不可為空1111")] public string UserName { get; set; } } }
這樣,我們上述的問(wèn)題就迎刃而解了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.Net Core中使用枚舉類(lèi)而不是枚舉的方法
這篇文章主要給大家介紹了關(guān)于A(yíng)SP.Net Core中使用枚舉類(lèi)而不是枚舉的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.Net Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Asp.Net防止刷新重復(fù)提交數(shù)據(jù)的辦法
最近在用Asp.Net編寫(xiě)點(diǎn)東西時(shí)遇到個(gè)問(wèn)題:即用戶(hù)在提交表單后按刷新就會(huì)重復(fù)提交數(shù)據(jù),即所謂的“刷新重復(fù)提交”的問(wèn)題。2013-03-03ASP.NET數(shù)據(jù)綁定之DataList控件
這篇文章主要為大家介紹了ASP.NET數(shù)據(jù)綁定中的DataList控件,DataList控件以表的形式呈現(xiàn)數(shù)據(jù),通過(guò)該控件,您可以使用不同的布局來(lái)顯示數(shù)據(jù)記錄,對(duì)DataList控件感興趣的小伙伴們可以參考一下2016-01-01ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之總體概述(一)
這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之總體概述,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08asp.net repeater實(shí)現(xiàn)批量刪除時(shí)注冊(cè)多選框id到客戶(hù)端
repeater批量刪除時(shí)注冊(cè)多選框id到客戶(hù)端的實(shí)現(xiàn)代碼2008-11-11asp.net下獲取Excel所有的工作表名稱(chēng)
asp.net下獲取Excel所有的工作表名稱(chēng)...2007-03-03asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁(yè)下載速度的代碼
剛看到有人給出asp.net實(shí)現(xiàn)的計(jì)算網(wǎng)頁(yè)下載速度的方法,本方法未經(jīng)本人測(cè)試,不知道能否可靠性如何。準(zhǔn)確來(lái)說(shuō),這只是個(gè)思路吧2013-03-03一個(gè)簡(jiǎn)單的ASP.NET Forms 身份認(rèn)證的實(shí)例方法
當(dāng)訪(fǎng)問(wèn)默認(rèn)首頁(yè)default.aspx時(shí),會(huì)自動(dòng)跳轉(zhuǎn)到login.aspx頁(yè)面上請(qǐng)求登錄,隨便輸入用戶(hù)名和密碼,點(diǎn)擊“登錄”按鈕,會(huì)回到首頁(yè),并顯示當(dāng)前登錄的用戶(hù)名。2013-07-07