Part 80 StringLength attribute in asp net mvc

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video, we will discuss using StringLength attribute. This attribute is present in System.ComponentModel.DataAnnotations namespace and is used to enforce minimum and maximum length of characters that are allowed in a data field. Let's understand this with an example.

We will be using table tblEmployee for this demo.
Create table tblEmployee
(
Id int primary key identity(1,1),
Name nvarchar(50),
Email nvarchar(50),
Age int,
Gender nvarchar(50)
)

Generate ADO.NET entity data model for table tblEmployee. Change the entity name from tblEmployee to Employee. Save and build the project.

Right click on the "Controllers" folder and select Add - Controller. Set
Name = HomeController
Template = MVC controller with read/write actions and views, using Entity Framework
Model class = Employee(MVCDemo.Models)
Data Context Class = EmployeeContext(MVCDemo.Models)
Views = Razor

To validate data, use validation attributes that are found in System.ComponentModel.DataAnnotations namespace. It is not a good idea, to add these validation attributes to the properties of auto-generated "Employee" class, as our changes will be lost, if the class is auto-generated again.

using System.ComponentModel.DataAnnotations;
namespace MVCDemo.Models
{
[MetadataType(typeof(EmployeeMetaData))]
public partial class Employee
{
}

public class EmployeeMetaData
{
[StringLength(10, MinimumLength = 5)]
[Required]
public string Name { get; set; }
}
}

Notice that, we have decorated "Name" property with "StringLength" attribute and specified Minimum and Maximum length properties. We also used [Required] attribute. So, at this point Name property is required and should be between 5 and 10 characters.

Points to remember:
1. [StringLength] attribute is present in System.ComponentModel.DataAnnotations namespace.
2. [StringLength] attribute verifies that a string is of certain length, but does not enforce that the property is REQUIRED. If you want to enforce that the property is required use [Required] attribute.

We will discuss the following attributes in our upcoming video sessions.
RegularExpression
Range
Рекомендации по теме
Комментарии
Автор

sir last one years i follow your tutorial. this is one of the best tutorial for me. your videos are helpful for me. i thank you sir from core of my heart for this pretty gift....

JaswantSingh-goey
Автор

Sure, will upload very soon. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

Csharp-video-tutorialsBlogspot
Автор

Hi Saagar, sure, will do it as soon as I can.

Csharp-video-tutorialsBlogspot
Автор

What is the reason to use Metadata and then link the partial class? Can't we apply attributes directly in our own Employee partial class?

rupenanjaria
Автор

Also how to make the error message go if a valid entry is made against a validation error and focus is moved to some other control ?

sunnyraj
Автор

Sir I am regularly following your tutorials. It very well explained by you.
sir i am waiting the the tutorial on MVC scaffolding. i am very confused on this topic.i searched many tutorials but not find satisfactory result. I am waiting detailed tutorial on this topic. Plz help me
Thanks sir.

bheesham
Автор

Good tutorials, really helpful Could you please upload a videos about Design patterns in .NET it really helpful for people who are giving interviews.

TheTruRavi
Автор

Hi Sir,
Data annotation are client side validations or server side validations

vjprakash
Автор

Any idea how to implement drop down list from database using ajax in MVC on a registeration form?

arun
Автор

Hi Sir,
Kindly release a video demonstrating Jquery implementation in mvc.

ziasixshaikh
Автор

How to do validations like "username already exists" by checking records from database using ajax without posting to the server ?

sunnyraj
Автор

Visual studio2015 not genetate explictly class agains partial class?any solution bcoz ifi create class error prompt already class avail in root directory

chandrakantnazirkar
Автор

It would have been good here if you could have shown the source code for the Edit view, just so people could see how the scaffolding process adds the tags necessary for validation. That's really the bit that people need to understand, otherwise it's just smoke and mirrors.

scaywagg
Автор

sir i want to send a TEXT msg through asp.net program..help me out how to send text msg ???

saagarsoni
Автор

resolved the problem


somehow in VS2013 employee.cs already exist

so when I add a NEW CLASS

I called it employee2.cs,  problem  vanish -- everything oke;

move to lesson 81

woodadmin-
Автор

yup the scaffolding was the only thing ifailed to understand also haha

Squackles
Автор

Sure, will do it as soon as I can. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

Csharp-video-tutorialsBlogspot
visit shbcf.ru