ASP.NET网页(剃刀)中的验证已弃用? [英] Validation in ASP.NET Web Pages (Razor) deprecated?

查看:62
本文介绍了ASP.NET网页(剃刀)中的验证已弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在剃须刀中使用验证,但是当我尝试使用该行时

I'm trying to use Validation in razor, but when I try to use the line

Validation.RequireFields("firstName", "lastName", "dateOfBirth");

视觉工作室告诉我:

'System.Web.Helpers.Validation'已过时:'"Use改为使用System.Web.HttpRequest.Unvalidated.'

'System.Web.Helpers.Validation' is obsolete: '"Use System.Web.HttpRequest.Unvalidated instead."'

"System.Web.Helpers.Validation"不包含以下内容的定义:'RequireFields'

'System.Web.Helpers.Validation' does not contain a definition for '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.

我如何进行验证?

谢谢!

如果相关,这是我的web.config:

Here's my web.config in case it's relevant:

<?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.

这篇关于ASP.NET网页(剃刀)中的验证已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆