Friday, 30 September 2016

Chapter-4 Asp.Net MVC Action





Asp.Net MVC Action : is responsible to execute the request and responsible for its response. It generate the response in the form of ActionResult.

Request Processing
1)      When ever any URL arrives it is configured within the routing Table who knows How to handle the URL request



2)      UrlRoutingModule information which configured in routing table and hands to MVC route table.
3)      MVC route handler passes the controller
4)      MvcHandler uses a controller factory to instantiate the controller and it knows what
5)      controller to instantiate because it looks in the RouteData for that controller value.
6)      Execute methodcreates an action invoker and tells that action invoker
7)      Action invoker, again, looks in the RouteData And finds that action parameter

Types of Action


Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Mvc;
Namespace MVCControllerDemo.Controllers
{
Public class EmployeeController: Controller
{
// GET: Employee
Public ActionResult Search(string name)
{
Var input = Server.HtmlEncode(name);
Return Content(input);
}
}
}


MVC and ASP.NET Diagram

Working with Views

  1. A view is a combination of HTML markup and code that runs on the Web server.
  2. The code is embedded in the Web pages using the Razor syntax.
  3. A Web application contains multiple views, where each view is responsible for either displaying some information to users or taking inputs from a user.
Specifying the View for an Action

1.      Once a request is processed, a controller renders an appropriate view that is shown to the user.
2.      For this, an action method needs to return a view.
   
Example:

You can return a view from within the Index() action method of the Home controller as shown in the following code snippet:

public class HomeController : Controller
         {
           public ActionResult Index()
           {
             return View();
           }
         }

1)      When a view is returned without specifying any name for the view MVC searches for the view:
i)        With the same name as the action method
ii)      Within the /Views/<ControllerName> folder

You can also specify the name of the view as a parameter of the View() method, as shown in the following code snippet:   

public class HomeController : Controller
   
        public ActionResult Index()
        {
           return View("NewIndex");
        }
    }

You can also render a view defined in a different folder other than the default folder created by MVC for every controller, as shown in the following code snippet:

public class HomeController : Controller
    public ActionResult Index()
    {
      return View("~/Views/Special/Index.cshtml");
    }
}

Passing Data from a Controller to a View

The following two common techniques are used to pass data from a controller to a view:
1)      ViewData:

a)      Is a dictionary of objects derived from the ViewDataDictionary class.
b)      Uses key/value pairs in the controller action method to set values, as shown in the following code snippet:

                    ViewData["Message"] = "Welcome to our website";
                    ViewData["ServerTime"] = DateTime.Now;

2)      Allows to access the added values in a view by using the Razor syntax, as shown in the following code snippet:
                    <p>
                    The message is: @ViewData["Message"]
                    The Date and Time is: @ViewData["ServerTime"]
                    </p>

3)      ViewBag:

      a)      Allows to add properties of multiple types and their values in a controller action method as shown in the following code snippet:

                   ViewBag.Message = "Welcome to our website";
                   ViewBag.ServerTime = DateTime.Now;
      
      b)      Allows to access the added values in a view as shown in the following code snippet:
                    <p>
                    The message is: @ViewBag.Message
                    The Date and Time is: @ViewBag.ServerTime
                    </p>

4)      Works only in those cases where the key being accessed is a valid C# identifier.

Using Partial Views
A partial view:
1)      Is a part of a view that can be used on multiple pages.
2)      Is created in the /Views/Shared folder.
3)      Name is prefixed with an underscore ( _ ) character.
4)      Can be rendered in a view by using the @Html.Partial() method.
5)      The following code snippet renders a partial view named _partialsummary within a view:

<div>@Html.Partial(“_partialsummary”)</div>

Working with Models
A model:
1)      Manages all the tasks related to handling data.
2)      Represents the data and the business logic of an application.
3)      Consists of a set of classes, where the objects of the classes represent the data associated with the application.
4)      Suppose you have the data of products, which includes product name, description, and price.
5)      To represent this data, you need to create a model class named Product with properties, such as Id, name, description, and price.
The structure of the model class should be similar to the structure of the Product table in the database as shown in the following figure.


6)      The direct mapping between the properties of the Product class and  the fields of the Product table is called object-relational mapping.

Accessing a Model Within a Controller

      1)      You need to create an object of the model class in the Product action method, as shown in the following code snippet:
        public ActionResult Product()
        {
            var product = new Newbay.Models.Product();
            product.name = "Samsung 5676 Mobile";
            product.description = "Newly launched with touch screen";
            product.price = 15000;
            return View();
       

       2)      To refer to the Product model class, the qualifier, Newbay.Models, is used.
3)      This qualifier can be omitted by including the namespace, Newbay.Models, at the top of the controller class, as shown in the following code snippet:
 
                using Newbay.Models

Passing Model Data from a Controller to a View

1)      Model data needs to be made accessible to a view so that it can be displayed to the users.
2)      For this, the controller can pass the model data to the view at the time of invoking the view.
3)      Model data
i)        Single object
ii)      Collection of objects
4)      You can pass the model data from a controller to a view by using ViewBag.

Example:

       public ActionResult Product(){
       var product = new Product();
       product.name = "Samsung 5676 Mobile";
               product.price = 15000;
       ViewBag.product = product;
       return View();
     }

5)      You can access the passed data on the corresponding view with the help of ViewBag, as shown in the following code snippet:

                        @ViewBag.product.name<br/>
                        @ViewBag.product.price<br/>

6)      Alternatively, you can pass the data to a view by passing the object as a parameter to the view, as shown in the following code snippet:

    public ActionResult Product()
        {
                          var product = new Product();
          product.name = "Samsung 5676 Mobile";
                   product.price = 15000;   
                           return View(product);
        }

7)      When you pass data object as a parameter to the view, you can use @Model to access the passed data on corresponding view.

       Example:

                        @Model.name <br/>
                        @Model.price <br/>

8)      Similar to a single object, you can pass a collection of objects from a controller to a view by using ViewBag.

      Example:

            public ActionResult Productlist()
                        {
                        var products = new List<Product>();
                                    var product1 = new Product();
                                    product1.name = "Samsung 5676 Mobile";
                        product1.price = 15000;
                                    var product2 = new Product();
                        product2.name = "Dell Inspiron 1545 laptop";
                        product2.price = 40000;         
                        products.Add(product1);
                        products.Add(product2);
                        ViewBag.products = products;
                        return View();
                        }

9)      To access the passed data on corresponding view, you need to iterate through the collection of objects stored in  ViewBag.
       
       Example:

            @{
            var products = ViewBag.products; 
         }
         @foreach (var p in products)
          {
                        @p.name<br />
                                    @p.price<br />
                                <br />
          }
10)  Alternatively, you can pass the data to a view by passing a collection of objects as a parameter to the view.
Example:
            public ActionResult Productlist()
                        {
                        var products = new List<Product>();
                                    var product1 = new Product();
                        product1.name = "Samsung 5676 Mobile";
                                    product1.price = 15000;        
                                    var product2 = new Product();
                                    product2.name = "Dell Inspiron 1545 laptop";
                                    product2.price = 40000;        
                                    products.Add(product1);
                                    products.Add(product2);
            return View(products);
                        }

11)  When you pass collection of objects as a parameter to the view, you can use model to access the passed data on corresponding view.

      Example:

            @{
                var products = Model;
         }
        @foreach(var p in products)     
        {
          @p.name <br />
          @p.price <br />
          <br/>
        }

Summary :

1)      Asp.Net MVC Action : is responsible to execute the request and responsible for its response. It generate the response in the form of ActionResult.
2)      The following two common techniques are used to pass data from a controller to a view:
a)      ViewData
b)      ViewBag
3)      ViewData is a dictionary of objects derived from the ViewDataDictionary class.
4)      ViewBag is just a dynamic wrapper over the ViewData dictionary.
5)      A partial view is a part of a view that can be used on multiple pages. 
6)      You can render a partial view in a view by using the @Html.Partial() method.
7)      In an MVC application, the component that enables interaction with data is called model.
8)      A model consists of a set of classes, where the objects of the classes represent the data associated with the application.




No comments:

Post a Comment