asp.net · ebook

[ASP.NET] Performing validation

Step 1: Decorate property with validation attribute

public class GuestResponse
    {
        [Required(ErrorMessage = "Please enter your name")]
        public string Name { get; set; }
 
        [Required(ErrorMessage = "Please enter your email")]
        public string Email { get; set; }
 
        [Required(ErrorMessage = "Please enter your phone")]
        public string Phone { get; set; }
 
        [Required(ErrorMessage = "Please enter your answer")]
        public bool? WillAttend { get; set; }
    }

Step 2: Add validation summary server control

<form id="rsvpform" runat="server">
        <strong><asp:ValidationSummary runat="server" 
ShowModelStateErrors="True" ID="validationSummary"/></strong>
        
            Your name:                       
...     </form>

Source: ASP.NET 4.5 Pro Apress – Chapter 1

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s