NuGet Package Management in ASP.NET MVC

Hello friends, I hope you all are doing great and having fun with your lives. In today's tutorial, we will discuss in detail about NuGet Package Management in ASP.NET MVC. It's 15th tutorial in ASP.NET MVC series. Today's tutorial is not about programming, instead we are gonna discuss this pre installed tool NuGet Package Management in ASP.NET MVC. NuGet Package Management is use to install packages and libraries, which you want to use in your project. It downloads the files online and then installs it. So, let's discuss this ASP.NET MVC tool in detail:

NuGet Package Management in ASP.NET MVC

  • NuGet Package Management is a package manager for ASP.NET MVC, which is used for downloading and installing different packages & Libraries online.
  • If any of your packages needs to be updated, then it can also be done by NuGet Package Management.
  • There are many third part packages available online, even there are many independent packages developed by Microsoft itself, which you can easily download & install using NuGet Package.
  • These third party packages could be open source or closed source.
  • So, now let's have a look at How to install any package using NuGet Package Management in ASP.NET MVC.
  • Right Click on your Projects' name in Solution Explorer and then click on Manage NuGet Packages, as shown in below figure:
  • It will open up a NuGet window in your workspace, as shown in below figure:
  • You can see in above figure that NuGet window is opened and it has three tabs:
    • Browse: For browsing new packages.
    • Installed: Search pre-installed packages in your visual studio.
    • Updates: Here you will get the notifications for updates of your pre-installed packages.
  • On the right side of each package, we have two versions listed.
  • The upper version is the installed version of that package in your visual studio software and the lower one is currently available version.
  • You can click on the upper arrow to update that package.
  • Here's the video demonstration of NuGet Package Management in ASP.NET MVC:
So, that was all about NuGet Package Management in ASP.NET MVC. It was a quick tutorial as I don't have much to explain here, its a simple tool but you should know about it as it will be used in coming tutorial. That's why I have posted it. If you have any questions, please ask in comments and I will try my best to help you out. Thanks for reading, have a good day. Take care !!! :)

Validation in ASP.NET MVC

Hello friends, I hope you all are doing great. In today's tutorial, we will discuss Validation in ASP.NET MVC. It's our 14th tutorial in ASP.NET MVC series. In our previous tutorial, we have seen Model Binding in ASP.NET MVC and you have seen we have created a View for our Model class. In today's tutorial, we are gonna add some validations on the data provided by the users in those Text box fields. We will place some constraints like the age must be above 13 or the characters of name must be between 4 to 20. So, let's have a look at How to deal with Validation in ASP.NET MVC:

Validation in ASP.NET MVC

  • Validation uses built-in attribute of ASP.NET called DataAnnotations and is used to validate inputs provided by the user.
  • Validation is a very powerful attribute of ASP.NET MVC and it can be applied to entire model class or any property of model class.
  • It places a restriction on the inputted data by the user, and if the data doesn't follow the specified rules then it will generate an Error Message and won't allow the user to submit form.
  • It's really helpful from development point of view, as the developer always gets the data in single specified format, thus can easily handle it.
  • Following table shows few of the most commonly used Validations in ASP.NET MVC:
Validation in ASP.NET MVC
No. Attribute Description
1 Range Value sould lie b/w specified range (e.g. Age)
2 StringLength Maximum Characters (e.g. First Name)
3 Required This field is Compulsory. (*required)
4 RegularExpression The user input must match the provided format (e.g. Date / Time)
5 MaxLength Sets the Maximum Limit.
6 CreditCard For Credit Card details.
7 CustomValidation Custom validations defined by developer.
8 FileExtension Restriction on File Extension (e.g. only img file)
9 MinLength Sets the Minimum Limit.
10 EmailAddress Email Address must be provided.
11 Phone Phone Number Format must followed.
  • I hope you have now got the basic idea of what is validation.
  • Now let's implement one of these validations in ASP.NET MVC.
  • So, open your StudentModel.cs file, it's our model class which we have created in tutorial: How to Create a New Model in ASP.NET MVC.
  • You can see in the above figure that I have added an attribute before Name variable.
  • It's a StringLength Validation and the first parameter which I have set to 20 is the maximum length of string allowed.
  • While minimum length is specified as 4, so this Validation is restricting the Name model to only accept value of length between 4 and 20.
  • So, now run your application and check the View and in Name Text box enter your name, here are the results:
  • In Case 1, the input string length is less than 4, while in Case 2, length is greater than 20.
  • That's why it has given error in both cases.
  • We can also add multiple Validation to single property, as shown in below figure:
  • Below this StringLength, I have placed another Validation in ASP.NET MVC which is [Required].
  • So, now it will also make sure that Name field is not empty.
  • Here's the video demonstration of Validation in ASP.NET MVC, have a look at it:
So, that was all about Validation in ASP.NET MVC. I hope you have enjoyed today's tutorial and I would suggest you to try all those Validation as a homework and share your codes with us in the comments. Thanks for reading, Take care !!!

Model Binding in ASP.NET MVC

Hello friends,I hope you all are doing great and having fun with your lives. In today's tutorial, we are gonna have a look at Model Binding in ASP.NET MVC. It's our 13th tutorial in ASP.NET MVC series. Today, we are gonna discuss a new concept in ASP.NET MVC and it's more of a luxury as it automates the job and makes our work easy. Suppose, in your application, you want to create a registration form and thus you need to pass this data from View to your model, so that it could be saved in your database. So, in order to bind this HTTP request data from View to Model, we use Model Binding. Let's have a look at Model Binding in ASP.NET MVC in detail:

Model Binding in ASP.NET MVC

  • Model Binding in ASP.NET MVC is used to bind the data sent by the HTTP request with Model.
  • You must have viewed during some form submission online, that when you click on the submit button then a form of data is sent by the HTTP string.
  • This HTTP data is linked to the Controller's action methods in the form of input parameters.
  • For example, suppose we have an action method "public ActionResult Edit(int id)", here the parameter int id could be used as a model binder.
  • It may happen that this id value is coming from some HTTP request "http://localhost/Student/Create?id=10".
  • So, the model binders get data from View via HTTP request and then pass this value to Controller's action method, which in turn shows up to models.
  • When we create a new form in ASP.NET MVC, then this model binding is automatically done by visual studio.
  • Let's create a new View and have a look at working of Model Binding in ASP.NET MVC.
  • So, in your StudentController.cs file, right click on the action method Create and then click on Add View, as shown in below figure:
  • When you click on the Add View, it will open a New Window for Creating a View.
  • We have already seen it in How to Create a New View in ASP.NET MVC.
  • So, here's the screenshot for the settings, first I have given this View a name Create, then I have selected Create Template.
  • Finally, I have selected the Model class, which we have created in one of our previous tutorial: Create a New Model in ASP.NET MVC.
  • After these settings, click the Add Button and a new View for the Create action method will be created.
  • Create.cshtml file will open up in your workspace, as shown in below figure:
  • If you remember the Student Model, it has four variables as shown in below figure:
  • So, let's open our newly created Model in the browser.
  • If everything goes fine then you will get similar results:
  • You can see in the above figure that visual studio has created the same four fields in the View which were present in the Model class.
  • The data we will enter here will be sent to the model and then will be saved in the database.
  • Code for the Model Binding in ASP.ENT MVC has automatically been created, which we have seen in Create.cshtml.
  • Here's the video demonstration of Model Binding in ASP.NET MVC:
So, that was all about Model Binding in ASP.NET MVC. If you got into any trouble in understanding it, then ask in comments. In the next tuorial, we will have a look at Data Validation in ASP.NET MVC. Thanks for reading, Take Care !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir