Thursday 8 January 2015

Validation in MVC 4 Razor with the Data Annotation Validators in “MODEL” in VB.NET

Hi friends,in this article I will explain about Validation in MVC4 Razor with the Data Annotation Validators in “MODEL”.
I already explained in the previous articles about Bind,Save,Edit,Update,Cancel,Delete,Paging example in GridView in ASP.NET using VB.NET,How to insert multiple rows into the GridView without using any database in ASP.NET using VB.NET/C# || how to save multiple rows in GridView into a Session.

Create MVC4 project and Add Model name it as UserDetails.
Models-->Add--->Class-->UserDetails.cs
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.ComponentModel.DataAnnotations
Namespace MVCTest.Models
    Public Class UserDetails
        <Required()> _
        <Display(Name:="First Name")> _
        <Required(ErrorMessage:="First Name Is Required")> _
        Public Property FirstName() As String
            Get
                Return m_FirstName
            End Get
            Set(ByVal value As String)
                m_FirstName = Value
            End Set
        End Property
        Private m_FirstName As String
        <Required()> _
        <Display(Name:="Last Name")> _
        <Required(ErrorMessage:="Last Name Is Required")> _
        Public Property LastName() As String
            Get
                Return m_LastName
            End Get
            Set(ByVal value As String)
                m_LastName = Value
            End Set
        End Property
        Private m_LastName As String
        <Required()> _
        <Display(Name:="Address")> _
        <StringLength(50)> _
        <Required(ErrorMessage:="Address required")> _
        Public Property Address() As String
            Get
                Return m_Address
            End Get
            Set(ByVal value As String)
                m_Address = Value
            End Set
        End Property
        Private m_Address As String
        <Required()> _
        <Display(Name:="DOB")> _
        <DataType(DataType.[Date])> _
        Public Property DOB() As DateTime
            Get
                Return m_DOB
            End Get
            Set(ByVal value As DateTime)
                m_DOB = Value
            End Set
        End Property
        Private m_DOB As DateTime
        <Range(100, 1000000)> _
        Public Property Salary() As Decimal
            Get
                Return m_Salary
            End Get
            Set(ByVal value As Decimal)
                m_Salary = Value
            End Set
        End Property
        Private m_Salary As Decimal

    End Class
End Namespace




and take one View name it as UserDetails.cshtml
@model MVCTest.Models.UserDetails

@{
    ViewBag.Title = "UserDetails";
}

<h2>UserDetails</h2>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval");
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
<table>

<tr> <td>
Your Name: @Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</td> </tr>
<tr> <td>
Your Age: @Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</td></tr>
<tr> <td>
Your Email: @Html.TextBoxFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</td></tr>
<tr><td>
Your Phone: @Html.TextBoxFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB)
</td></tr>
<tr><td>
Your Address: @Html.TextBoxFor(model => model.Salary)
@Html.ValidationMessageFor(model => model.Salary)
</td></tr>
<tr><td>
<input type="Submit" value="Register Me" />
</td></tr>
</table>

}


and Run the application.The output of the above will like as shown in the figure.


No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.