Attach single view with multiple actions.

Sourav.Kayal
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 2774 red flag

How to attach single view with multiple action

Attach single view with multiple actions.

In this article we will learn how to attach single view with multiple actions.  It’s common situation in project development where we don’t want to create view for each and every action of certain controller. In this situation, we have to use concept of shared view. The solution is very simple; just we have to keep the view in shared folder. Like below.

 

Now, The question is why need to keep in shared folder?. The reason is when we run any controller, by default it check its own directory or shared directory.  Every controller will look in shared directory, if it not available in it’s own directory. Let’s have a look on below code.

Here we will create very simple controller class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Action1()
        {
            ViewBag.Controller = "Action1";
            return View("_Common");
        }
        public ActionResult Action2()
        {
            ViewBag.Controller = "Action2";
            return View("_Common");
        }
 
    }
}

Both Action1() and Action2() is calling _Common view and as the view is placed in shared folder, both can able to access. Before calling to view we are assigning action name in ViewBag. From view we can detect which action has invoked it.

Here is code for view

<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<MVC3.Models.customer>"
%>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>_Common</title>
</head>
<body>
    <div>
        <% var ActionName = ViewBag.ActionName; %>
        <% if (ActionName == "Action1")
           {%>
            This view is called from Action 1
           <%}
           else
           {%>
            This view is Called from Action 2
           <%} %>
    </div>
</body>
</html>
 

Here is output.



Now, the question may arise “Is it possible to call one view from different controller”? Yes, we can call. In below example, we will see how to do that.

Call one view from different controller

This is our first controller Home1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class Home1Controller : Controller
    {
        public ActionResult Action()
        {
            ViewBag.Controller = "Home1";
            return View("_Common");
        }
    }
}
 

Here is Home2 controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class Home2Controller : Controller
    {
        public ActionResult Action()
        {
            ViewBag.Controller = "Home2";
            return View("_Common");
        }
 
    }
}
 

It’s clear that both Action() (they are in different controller) are calling same view. And here is output.

 


Conclusion

Here we have discuss how to attach single view with multiple actions. Hope you have understood the concept.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)