一、一般情况
对于使用过MVC框架的人来说,对MVC的数据验证不会陌生,比如,我有一个Model如下:
1 public class UserInfo2 {3 [Required(ErrorMessage = "UserName不可为空1111")]4 public string UserName { get; set; }5 public string Sex { get; set; }6 public string Mobile { get; set; }7 public string Address { get; set; }8 }
前端:
1 @using (Html.BeginForm()) 2 { 3 @Html.AntiForgeryToken() 4 <div class="form-horizontal"> 5 <h4>UserInfo</h4> 6 <hr /> 7 @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 8 <div class="form-group"> 9 @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })10 <div class="col-md-10">11 @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })12 @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })13 </div>14 </div>15 <div class="form-group">16 @Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })17 <div class="col-md-10">18 @Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } })19 @Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })20 </div>21 </div>22 <div class="form-group">23 @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })24 <div class="col-md-10">25 @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })26 @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })27 </div>28 </div>29 <div class="form-group">30 @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })31 <div class="col-md-10">32 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })33 @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })34 </div>35 </div>36 <div class="form-group">37 <div class="col-md-offset-2 col-md-10">38 <input type="submit" value="Create" class="btn btn-default" />39 </div>40 </div>41 </div>42 }
效果:
是的,MVC可以通过对一些属性添加一定的特性来对数据进行验证。这对大家来说可能并不陌生。
如果仅仅是这样就完事了,那么也就没事么意思了。
二、常用情况
在实际的开发中,我们大都是通过EF,或者其他方式,使得数据库中的每一个表或视图,都在代码中对应的一个类模型,对于通过数据库生成的模型,我们不宜修改,退一步讲,即使我们在这个类中对一些属性增加一些数据验证的特性,那么,数据库发生变化后,如果我再重新生成这些Model,我们之前添加好的验证特性将没有了,那么,我们如何解决这样的问题呢?
假如:
1 public class UserInfo2 { 3 public string UserName { get; set; }4 public string Sex { get; set; }5 public string Mobile { get; set; }6 public string Address { get; set; }7 }
UserInfo是通过数据库生成的一个模型,对于数据库生成的模型,我们不宜修改。但那是,我们又需要对这个模型中的某些属性进行数据验证,比如需要对UserName属性进行非空验证,那么我们如何做呢?
大家通常会想到部分类,是的,我们可以通过部分类来解决上述问题。
首先,我们将模型中的类加上关键字 partial ,然后我们再写一个这个模型的部分类。
1 public partial class UserInfo2 {3 [Required(ErrorMessage = "UserName不可为空1111")]4 public string UserName { get; set; }5 }
但是,这样会提示我们一个错误,就是类中存在重复的属性,是的,部分类中,属性是不可以重名的。那么,我们该怎么办呢,MVC框架已经给了我们解决方案了。
我们可以这么写:
1 [MetadataType(typeof(MeteUserInfo))]2 public partial class UserInfo3 {4 private class MeteUserInfo5 {6 [Required(ErrorMessage = "UserName不可为空1111")]7 public string UserName { get; set; }8 }9 }
这样,我们上述的问题就迎刃而解了。
以上是总结MVC中数据验证实例的详细内容。更多信息请关注PHP中文网其他相关文章!

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通过不断的更新和优化,适应了新兴技术的需求。1)C#9.0和.NET5引入了记录类型和性能优化。2).NETCore增强了云原生和容器化支持。3)ASP.NETCore与现代Web技术集成。4)ML.NET支持机器学习和人工智能。5)异步编程和最佳实践提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。

C#.NET应用的测试策略包括单元测试、集成测试和端到端测试。1.单元测试确保代码的最小单元独立工作,使用MSTest、NUnit或xUnit框架。2.集成测试验证多个单元组合的功能,常用模拟数据和外部服务。3.端到端测试模拟用户完整操作流程,通常使用Selenium进行自动化测试。

C#高级开发者面试需要掌握异步编程、LINQ、.NET框架内部工作原理等核心知识。1.异步编程通过async和await简化操作,提升应用响应性。2.LINQ以SQL风格操作数据,需注意性能。3..NET框架的CLR管理内存,垃圾回收需谨慎使用。

C#.NET面试问题和答案包括基础知识、核心概念和高级用法。1)基础知识:C#是微软开发的面向对象语言,主要用于.NET框架。2)核心概念:委托和事件允许动态绑定方法,LINQ提供强大查询功能。3)高级用法:异步编程提高响应性,表达式树用于动态代码构建。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版