Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • .Net MVC Controllers and Simple Model Validation

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 348
    Comment on it

    .Net MVC Controllers

    The controller is a heart of the entire MVC architecture which inherits system.web.mvc.controller. The controller acts as a coordinator between the View and the Model as it receives input from users via the View, then process the user's data with the help of Model and passes the results back to the View. Moreover, controllers in Asp.Net MVC, respond to HTTP requests and determine the action to take based upon the content of the incoming request. By default, controllers are stored in the Controllers folder of the project. Hence Controller is responsible for controlling the application logics..

    The Controller is responsible for the following stages:

    1. Locating the appropriate action method to call and validating that it can be called.
    2. Getting the values to use as the action method's arguments.
    3. Handling errors that might occur during the execution of action method.
    All controller classes must be named by using the "Controller" suffix. This controller class contains action methods that are used to render view pages.

    We use one of the following methods of the Controller base class:
    1. View - It returns a ViewResult action result.
    2. Redirect -It returns a RedirectResult action result.
    3. RedirectToAction - It returns a RedirectToActionResult action result.
    4. RedirectToRoute - It returns a RedirectToRouteResult action result.
    5. Json -It returns a JsonResult action result.
    6. JavaScriptResult - It returns a JavaScriptResult.
    7. Content - It returns a ContentResult action result.
    8. File - It returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

    Below is the example of controller:

    public class AdminController: Controller
        public ActionResult Index()
            ViewData["Message"] = "Welcome Admin!";
            return View();

    Model Validation

    In ASP.NET MVC, model validations are done by using Data Annotations. Data Annotations are special attributes that are applied to a class or properties of a class. The advantage of using the Data Annotation validators is that they enable you to perform validation simply by adding one or more attributes such as the Required or StringLength attribute to a class property.
    In order to use the Data Annotations Model Binder in an ASP.NET MVC application, we need to add a reference to the Microsoft.Web.Mvc.DataAnnotations.dll assembly and the System.ComponentModel.DataAnnotations.dll assembly.
    Here below is the sample example of Model Validation in which we use [Required],[Range],[RegularExpression] validation:
    1. Range It enables you to validate whether the value of a property falls between a specified range of values.
    2. ReqularExpression It enables you to validate whether the value of a property matches a specified regular expression pattern.
    3. Required It enables you to mark a property as required.
    Suppose we have a class customer

    public class Customer
        [Required(ErrorMessage="Please enter Name")]
        public string customername{get;set;} 
         [Required(ErrorMessage="Please enter email id")]
         [RegularExpression(^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$", Please enter valid email id")]
       public string customeremail{get;set;}
       [Range(10,120,ErrorMessage="Age must be between 10 to 120")]
       public int age{get;set;}

    In View,

    @using (Html.BeginForm()) {
    Name:@Html.TextBoxFor(model => model.Name)
         @Html.ValidationMessageFor(model => model.Name)
    Emailid:@Html.TextBoxFor(model => model.Email)
         @Html.ValidationMessageFor(model => model.Email)
    Age:@Html.TextBoxFor(model => model.Age)
         @Html.ValidationMessageFor(model => model.Age)

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: