Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Showing posts with label programming with shri. Show all posts
Showing posts with label programming with shri. Show all posts

Wednesday, September 4, 2019

Layout View In ASP.NET MVC 5

We will learn Layout views in MVC 5 and create Layout View using the default template in MVC 5. Also, we will learn about using the same Layout View in different view pages in ASP.NET MVC 5 with examples.
Layout View in ASP.NET MVC 5



In this article, you will learn the following points about Layout Views in MVC 5.

  • What is Layout View in MVC 5?
  • How to create Layout View using the default template in MVC 5?
  • How to use same Layout View in different view pages in MVC 5
  • How to use Partial View in Layout view?

Following are the previews two articles on ASP.NET MVC 5

What is Layout View in MVC 5?
  • Layout view is used for providing common look for all the different view pages.
  • Layout view is the same as a master page in asp.net web application.
  • The layout view extension is .cshtmland It uses the Razor view engine.
  • Layout View can contain HTMLand C#code.
  • While creating layout page prefix should be _and view name.

How to create Layout View using the default template in MVC 5?

Steps to create Layout View using the default template in MVC 5


Step 1:
First, create an ASP.NET MVC application using Visual Studio 2017 and provide the name “MVC5LayoutDemo”.

Layout View in ASP.NET MVC 5


Step 2:
Go to solution explorer => Views Folder => Right-click on “Shared” Folder >> go to “Add” >> Click on [New Item] as follow.

Layout View in ASP.NET MVC 5

Step 3:
Select “MVC 5 Layout Page(Razor)” template and provide the required name like “_LayoutProduct” click on “Add” button.

Layout View in ASP.NET MVC 5


Default code generated for _Layout View in MVC 5 default template as follow.
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>


Add code for custom header and footer in layout view page as per our requirement.

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
</head>
<body>
    <div class="row" style="background-color:azure;">
        This is custom header.
    </div>
    <div>
        @RenderBody()
    </div>
    
    <div class="row" style="background-color:goldenrod;">
        This is custom footer.
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

How to use same Layout View in different view pages in MVC 5

Create two new views using layout view and view name as “Product.cshtml” and “Order.cshtml” view.

Steps to create a view using the layout view.

Step 1:
Go to solution explorer => Views Folder => Right-click on “Home” Folder >> go to “Add” >> Click on [New Item] as follow.

Layout View in ASP.NET MVC 5


Step 2:
Select "MVC 5 View page with Layout(Razor)" template and provide the required name like "Product.cshtml" and click on "Add" button as follow.
Layout View in ASP.NET MVC 5


Step 3:
select "_LayoutProduct.cshtml" from "Shared" folder in Project folders on the Layout page window and click on the "Ok" button as follow.
Layout View in ASP.NET MVC 5

Html code for Product.cshtml view as follow.

@{
    Layout = "~/Views/Shared/_LayoutProduct.cshtml";
}

<h3>Product Detail </h3>
<table class="table table-responsive table-bordered table-striped">
   <thead>
       <tr class="btn-primary">
          <th>ProductId</th>
           <th>Product Name</th>
           <th>Quantity</th>
           <th>OrderId</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td>101</td>
           <td>ASP.NET MVC 5 BOOK</td>
           <td>2</td>
           <td>001</td>
       </tr>
       <tr>
           <td>101</td>
           <td>ASP.NET Core BOOK</td>
           <td>2</td>
           <td>002</td>
       </tr>
   </tbody>
</table>

Create Product action method in Home controller as follow. 

public ActionResult Product()
        {
            return View();
        }

Output:
Layout View in ASP.NET MVC 5


Create “Order.cshtml” view to follow the above steps and added the following code in order view.


@{
    ViewBag.Title = "Order";
    Layout = "~/Views/Shared/_LayoutProduct.cshtml";
}

<h2>Order Detail</h2>
<table class="table table-responsive table-bordered table-striped">
    <thead>
        <tr class="btn-primary">
            <th>OrderId</th>
            <th>Order Name</th>            
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>001</td>
            <td>ASP.NET MVC 5 BOOK</td>           
        </tr>
        <tr>
            <td>002</td>
            <td>ASP.NET Core BOOK</td>           
        </tr>
    </tbody>
</table>


Create Order action method in Home controller as follow.

public ActionResult Order()
        {
            return View();
        }


Output:
Layout View in ASP.NET MVC 5

References:

I hope you understand the concepts of layout view in ASP.NET MVC 5
Thanks for reading.


More detail watch follow video on Layout View In ASP.NET MVC 5:



Share:

Monday, May 6, 2019

Action Method In ASP.NET MVC 5


We will learn Action Method in ASP.NET MVC 5 with example and example of Action Method in ASP.NET MVC 5 with example in this article. The default configuration of the action method in asp.net MVC 5.

Action Method in ASP.NET MVC 5


Following are the previews article on ASP.NET MVC


Action Method in ASP.NET MVC 5:


ASP.NET MVC action methods are responsible to execute the request and generate a response on it. All the public method of MVC controller are action method. If we want the public method is non-action method then we can decorate the action method by “NonAction” attribute. It will restrict the action method to render on the browser.

To work with action method need to remember the following points. 

  • The action method is a public non-static method.
  • Action method cannot be a private or protected method.
  • Action method cannot contain ref and out parameters.
  • Action method cannot be an extension method.
  • Action method cannot be overloaded.

  

Example of an action method in ASP.NET MVC as follows.

Action Method in ASP.NET MVC 5




public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
}
You can see in the above action method example, the Index() method is a public method which is inside the HomeController class. And the return type of the action method is ActionResult it can return using View()method. So every public method inside the Controller is an action method in MVC. 


Action method can not be private or protected method: 

public class HomeController : Controller
{
    // GET: Home
    private string Index()
    {
        return "This is Index Method";
    }

    protected string Index1()
    {
        return "This is Index Method";
    }
}


If you provide the private or protected access modifier in then it will provide the error to the user I.e. “resource can not found” as follows.

Action Method In  MVC 5
Action method can not contain ref and out parameters: 

public class HomeController : Controller
    {
        public string Index(ref int id)
        {
            return "This is Index Method" + id;
        }
}

public class HomeController : Controller
{
    public string Index(out int id)
    {
        id = 0;
        return "This is Index Method" + id;
    }
}
   

We can not provide the ref and out parameters to the action method if you try to provide the ref and out parameters to the action method then it will throw an error as follow.

Action Method In MVC 5

Action method can not be overloaded:

public string Index()
        {
            return "This is Index Method";
        }
        public string Index(int i)
        {
            return "This is Index Method"+i;
        }
 If you try to overload the action method then it will throw an ambiguous error as follows.

Action Method In MVC 5


Type of Action method
  • Action Method
  • Non Action Method

How to call Action Method:

Syntax: YourDomainName/ControllerName/ActionMethodName

Example: localhost/Home/Index
Action Method In MVC 5
How to change default Action Method:

Default Action method is configured in RouteConfig.cs class. By default Action method is the “Index” action method. Change the “Index” action method name as per our requirement as bellow code.

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Message", id = UrlParameter.Optional }
            );
        }
    }


You can see the above code we have changed the action method name as “Message” instead of “Index” by default.

How to restrict the public action method in MVC:

To restrict the public action method in MVC. We can use “NonAction” attribute. The “NonAction” attribute exists in System.Web.Mvcnamespace. 

Example of NonAction attribute in ASP.NET MVC:

      [NonAction]
        public string GetFullName(string strFirstName, string strLastName)
        {
            return strFirstName + " " + strLastName;
        }
  

Default Action Method in MVC: 

Default controller and Action method are configured in RouteConfig.cs class. By default, Controller is the Home controller and default Action method is the “Index” action method.

Example of default action method as follows:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Interview questions for freshers on the Action Method,

  1. What is the Action Method in ASP.NET MVC/MVC 5?
  2. How to restrict the Action Method.
  3. How to call Action Method?
  4. How to change default Action Method?
  5. How does the default action get executed?
More detail watch follow video on Action Method in ASP.NET MVC 5:

ASP.NET MVC 5 Tutorials Step By Step:



References:


I hope you understand the concepts of an action method in asp.net MVC 5
Thanks for reading.
Share:

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave