Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘validation’

Server Side Validation With ASP.NET MVC

aspnet_mvc When you’re using ASP.NET MVC, there’s no validation controls for you compared to ASP.NET WebForms. It is always important to validate data on the server as well as the client. Back when Dynamic Data was being developed, a set of attributes was created to help tell the Dynamic Data folks about validation and other metadata so they could create smart scaffolds. What this allows you to do is decorate your classes or properties with validation attributes. A project that is available on CodePlex gives you the power to inject this validation into your MVC project. The CodePlex project can be found here. The end results are you can decorate your classes with some of the following attributes to perform server side validation:

  • Required – a required field
  • StringLength – allows you to set the minimum and maximum length of a string
  • RegularExpression – performs regular expression validation

When you use this your validation classes will look like the following example:

Via C#

   1: public class EmployeeMetaData

   2: {

   3: [Required]

   4:       [StringLength(10, ErrorMessage="Given name cannot be more than 10 characters")]

   5:       public string GivenName { get; set; }

   6:  

   7:       [Required]

   8:       public string Surname { get; set; }

   9: }

Via VB

   1: Public Class EmployeeMetaData

   2: Private privateGivenName As String

   3: <Required, StringLength(10, ErrorMessage:="Given name cannot be more than 10 characters")> _

   4: Public Property GivenName() As String

   5:       Get

   6:             Return privateGivenName

   7:       End Get

   8:       Set(ByVal value As String)

   9:             privateGivenName = value

  10:       End Set

  11: End Property

  12:  

  13:        Private privateSurname As String

  14:        <Required> _

  15:        Public Property Surname() As String

  16:              Get

  17:                    Return privateSurname

  18:              End Get

  19:              Set(ByVal value As String)

  20:                    privateSurname = value

  21:              End Set

  22:        End Property

  23: End Class

  • Digg
  • DZone
  • Yahoo Buzz
  • Delicious
  • Reddit
  • StumbleUpon
  • SmakNews
  • Jumptags
  • Ping
  • Share/Bookmark

ASP.NET MVC 2 and Visual Studio 2010

Unfortunately, because Visual Studio 2010 Beta 2 and ASP.NET MVC 2 Beta share components which are currently not in sync, running ASP.NET MVC 2 Beta on VS10 Beta 2 is not supported. Regarding Visual Studio 2010 and .NET 4 support, that is unfortunately not a feasible option. The most recent public release of VS2010 and .NET 4 is Beta 2. However, our internal builds of MVC 2 for VS2010 and .NET 4 depend on features that were available only after Beta 2. In other words, if we released what we have right now for VS2010 and .NET 4 then it wouldn’t even run.

The good news is that the Visual Studio 2010 Release Candidate will include a newer version of ASP.NET MVC 2. 

Highlights

As you might expect from a release candidate, most of the work focused on bug fixes and improvements to existing features. We also spent a lot of time on performance profiling and optimization.

Much of the focus on this release was in the client validation scripts. For example, the validation script was moved into its own file and can be included at the top or bottom of the page. Client validation also now supports globalization.

The other change related to validation is that the ValidationSummary now supports overloads where only model-level errors are displayed. This is useful if you are displaying validation messages inline next to each form field. Previously, these messages would be duplicated in the validation summary. With these new changes, you can have the summary display an overall validation message (ex. “There were errors in your form submission”) as well as a list of validation messages which don’t apply to a specific field.

What’s Next?

RTM of course! The RTM release of ASP.NET MVC will be included in the RTM release of Visual Studio 2010, which is slated for some time in March. The VS2008 version of ASP.NET MVC 2 might release earlier than that. We’re still working out those details.

  • Digg
  • DZone
  • Yahoo Buzz
  • Delicious
  • Reddit
  • StumbleUpon
  • SmakNews
  • Jumptags
  • Ping
  • Share/Bookmark