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."'
and
'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.
How do I do validation?
Thanks!
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>
Validating User Input in Razor Pages. When you allow users to provide values that you then process, you need to ensure that the incoming values are of the expected data type, that they are within the permitted range and that required values are present. This process is known as input validation.
An important aspect of creating ASP.NET Web pages for user input is to be able to check that the information users enter is valid. ASP.NET provides a set of validation controls that provide an easy-to-use but powerful way to check for errors and, if necessary, display messages to the user.
Introduction to Razor Pages in ASP.NET Core. Razor Pages is a new aspect of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive. If you're looking for a tutorial that uses the Model-View-Controller approach, see Get started with ASP.NET Core MVC. This document provides an introduction to Razor Pages.
Validating User Input. In ASP.NET Web Pages 2, you can use the Validator helper to test user input. The basic approach is to do the following: Determine which input elements (fields) you want to validate. You typically validate values in <input> elements in a form.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With