[英]Validation in ASP.NET Web Pages (Razor) deprecated?
I'm trying to use Validation in razor, but when I try to use the line 我正在尝试在剃刀中使用验证,但是当我尝试使用该线时
Validation.RequireFields("firstName", "lastName", "dateOfBirth");
visual studio tells me: 视觉工作室告诉我:
'System.Web.Helpers.Validation' is obsolete: '"Use System.Web.HttpRequest.Unvalidated instead."' 'System.Web.Helpers.Validation'已过时:'“使用System.Web.HttpRequest.Unvalidated代替。”
and 和
'System.Web.Helpers.Validation' does not contain a definition for 'RequireFields' 'System.Web.Helpers.Validation'不包含'RequireFields'的定义
but the most up to date reference I can find is http://www.asp.net/web-pages/overview/more-resources/asp-net-web-pages-api-reference#Validation and I don't really see how Unvalidated is supposed to do what I want. 但我能找到的最新参考是http://www.asp.net/web-pages/overview/more-resources/asp-net-web-pages-api-reference#Validation我真的不是看看Unvalidated应该如何做我想要的。
How do I do validation? 我该如何进行验证?
Thanks! 谢谢!
Here's my web.config in case it's relevant: 这是我的web.config,如果它是相关的:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="WebRole2" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
Do validation on your model with attributes: 使用属性对模型进行验证:
public class MyModel
{
[Required][StringLength(100)]
public string FirstName {get;set;}
}
See this excellent blog post which explains it quite nicely. 看到这篇优秀的博客文章 ,它很好地解释了它。 Also if the framework attributes don't fulfill your requirements, this library has a lot of extra attributes. 此外,如果框架属性不满足您的要求,则此库具有许多额外属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.