Friday, 15 April 2016

Validation and Data Annotations In MVC 5


Validation and  Data Annotations  In MVC 5

Validation is way to check data or information is authentic or not. Here we are discuses How to validate data or information from client Side Step By step
step 1:
use namespace 
System.ComponentModel.DataAnnotations;
step2:
Create Model class
Let Us consider Create Model Class employee
and some property define as 

public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string EmailConfirm { get; set; }
public int Age { get; set; }
public decimal Price { get; set; }


step3

We are Disused Required

 [Required]
public string FirstName { get; set; }

[Required]

public string LastName { get; set; }

[Required]

[StringLength(160)]
public string FirstName { get; set; }


[Required]

[StringLength(160)]
public string LastName { get; set; }

[Required]
[StringLength(160, MinimumLength=3)]
public string FirstName { get; set; }


Step 4:

RegularExpression

Some properties of Order require more than a simple presence or length check. For example, 
you want to ensure the Email property of an  Order contains a valid, working e-mail address. 
Unfortunately, ensuring an e-mail address is working without sending a mail message and waiting 
for a response is practically impossible. What you can do instead is ensure the value  looks  like  a 
working e-mail address using a regular expression:


[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }


Range
The  Range attribute specify  minimum and maximum constraints for a numerical value. If the 
 field  only wanted to serve middle-aged customers, you could add an Age  property to the 
Order class and use the  Range attribute as in the following code:


[Range(35,44)]
public int Age { get; set; }

The values are inclusive. The Range attribute can work with integers and doubles, and 
another overloaded version of the constructor takes a  Type parameter and two strings (which can 
allow you to add a range to date and decimal properties, for example).

[Range(typeof(decimal), "0.00", "49.99")]
public decimal Price { get; set; }


Compare:

Compare ensures two properties on a model object have the same value. For example, you might 
want to force customers to enter their e-mail address twice to ensure they didn’t make a typographi-
cal error:

[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }

[Compare("Email")]
public string EmailConfirm { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name="Password")]
public string Password { get; set; }


Remote

The ASP.NET MVC framework adds an additional Remote validation attribute. This attribute is in the  System.Web.Mvc namespace.
The  Remote attribute enables you to perform client-side validation with a server callback. Take, for example, the  UserName  property of the RegisterModel  class in the MVC Music Store. No two user should have the same  UserName  value, but validating the value on the client to ensure the value is unique is difi cult (to do so you would have to send every single username from the database to the client). With the Remote attribute you can send the  UserName  value to the server, and compare the value against the values in the

database.[Remote("CheckUserName","Account")]
public string UserName { get; set; }


Monday, 11 April 2016

ASP.NET MVC Attribute Routing

ASP.NET MVC Attribute Routing:

Attribute Routing is a new option for specifying routes by placing annotations on your controller classes or action methods.

ASP.NET Identity

ASP.NET Identity:

The membership and authentication systems in MVC 5 have been completely rewritten as part of the new ASP.NET Identity system. This new system moves beyond some outdated constraints of the previous ASP.NET Membership system, while adding some sophistication and configuration to the Simple Membership system that shipped with MVC 4.

Here are some of the top new features in ASP.NET Identity: 

➤ One ASP.NET Identity system: In support of the One ASP.NET focus we discussed earlier, the new ASP.NET Identity was designed to work across the ASP.NET family (MVC, Web Forms, Web Pages, Web API, Signal R, and hybrid applications using any combination).
➤ Control over user profile data:

Although it’s a frequently used application for storing additional, custom information about your users, the ASP.NET Membership system made doing it very difficult. ASP.NET Identity makes storing additional user information (for example, account numbers, social media information, and contact address) as easily as adding properties to the model class that represents the user.
➤ Control over persistence:

By default, all user information is stored using Entity Framework Code First. This gives you both the simplicity and control you’re used to with Entity Framework Code First. However, you can plug in any other persistence mechanism you want, including other ORMs, databases, your own custom web services, and so on.
➤ Testability:

The ASP.NET Identity API was designed using interfaces. These allow you to write unit tests for your user-related application code.
➤ Claims Based:

Although ASP.NET Identity continues to offer support for user roles, it also supports claims-based authentication. Claims are a lot more expressive than roles, so this gives you a lot more power and flexibility. Whereas role membership is a simple Boolean value (a user either is or isn’t in the Administrator role), a user claim can carry rich information, such as a user’s membership level or identity specifics.
➤ Login providers:

 Rather than just focusing on username / password authentication, ASP.NET Identity understands that users often are authenticated through social providers (for example, Microsoft Account, Facebook, or Twitter) and Windows Azure Active Directory.

➤ NuGet distribution:

ASP.NET Identity is installed in your applications as a NuGet package. This means you can install it separately, as well as upgrade to newer releases with the simplicity of updating a single NuGet package.

History of ASP.Net MVC

History of ASP.Net MVC:

ASP.NET MVC 1

ASP.NET MVC 1 was released in february 2007 byScott Guthrie of Microsoft sketched out the core of ASP.NET MVC while flying on a plane to a conference on the East Coast of the United States.

 It was a simple application, containing a few hundred lines of code, but the promise and potential it offered for parts of the Microsoft web developer audience was huge. As the legend goes, at the Austin ALT.NET conference in October 2007 in Redmond, Washington, ScottGu showed a group of developers “this cool thing I wrote on a plane” and asked whether they saw the need and what they thought of it. It was a hit. In fact, many people were involved with the original prototype, codenamed Scalene. Eilon Lipton e-mailed the fi rst prototype to the team in September 2007, and he and ScottGu bounced prototypes, code, and ideas back and forth.

ASP.NET MVC 2

ASP.NET MVC 2 was released  in March 2010.

Some of the main features in MVC 2 included:

 ➤ UI helpers with automatic scaffolding with customizable templates
 ➤ Attribute-based model validation on both the client and server
➤ Strongly typed HTML helpers
➤ Improved Visual Studio tooling
➤ Support for partitioning large applications into areas
➤ Asynchronous controllers support
➤ Support for rendering subsections of a page/site using Html.RenderAction
➤ Lots of new helper functions, utilities, and API enhancements

ASP.NET MVC 3 

ASP.NET MVC 3 released  just 10 months after MVC 2, driven by the release date for Web Matrix. 

Some of the top features in MVC 3 included: 

➤ The Razor view engine
➤ Support for .NET 4 Data Annotations
➤ Improved model validation
➤ Greater control and flexibility with support for dependency resolution and global action filters
➤ Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding
➤ Use of NuGet to deliver software and manage dependencies throughout the platform

ASP.NET MVC 4


The MVC 4 release built on a pretty mature base and is able to focus on some more advanced scenarios.
Some top features include:
➤ ASP.NET Web API
➤ Enhancements to default project templates
➤ Mobile project template using jQuery Mobile
➤ Display modes
➤ Task support for asynchronous controllers
➤ Bundling and minifi cation Because MVC 4 is still a pretty recent release, we explain a few of these features in a little more detail here and describe them in more detail throughout the book.



ASP.NET MVC 5 

MVC 5 was released along with Visual Studio 2013 in October 2013. The main focus of this release was on a “One ASP.NET” initiative (described in the following sections) and core enhancements across the ASP.NET frameworks.

Some of the top features include: 

➤ One ASP.NET
➤ New Web Project Experience
 ➤ ASP.NET Identity
➤ Bootstrap templates
 ➤ Attribute Routing
➤ ASP.NET scaffolding
➤ Authentication fi lters
➤ Filter overrides

Introduction of ASP.NET MVC

Introduction of ASP.Net MVC Model View Control:

ASP.NET MVC is a framework for building web applications that applies the general Model View Controller pattern to the ASP.NET framework.

Model-View-Controller (MVC) has been an important architectural pattern in computer science for many years. Originally named Thing-Model-View-Editor in 1979,

The MVC separates the user interface (UI) of an application into three main aspects:

 ➤ The Model: A set of classes that describes the data you’re working with as well as the business rules for how the data can be changed and manipulated

The View: Defines how the application’s UI will be displayed

The Controller: A set of classes that handles communication from the user, overall application fl ow, and application-specifi c logic


The MVC pattern is used frequently in web programming. With ASP.NET MVC.

 ➤ Models: These are the classes that represent the domain you are interested in. These domain objects often encapsulate data stored in a database as well as code that manipulates the data and enforces domain-specifi c business logic. With ASP.NET MVC, this is most likely a Data Access Layer of some kind, using a tool like Entity Framework or NHibernate combined with custom code containing domain-specifi c logic.

➤ View: This is a template to dynamically generate HTML.


➤ Controller: This is a special class that manages the relationship between the View and the Model. It responds to user input, talks to the Model, and decides which view to render (if any). In ASP.NET MVC, this class is conventionally denoted by the suffix Controller.