Menu

Friday 5 September 2014

View Result Return Type in MVC 5.0

About:

ViewResult is the one of the ActionResult type which will render the specified view as web Page. This class is inherited from the ViewResultBase class (PartialViewResult also inherited from the same class).

Examples:

You can use ViewResult Type as return type in six different ways. I am going to explain all those with simple code examples.

Example1:

This type you can return directly named as View(). In this scenario the controller action method name and view name are the same.

So, This example Action Controller method name and view name is: Example1

Code:
        
       /// 
        /// Returns the Default view which is named 
        /// as Action controller method name (i.e Example1.cshtml)
        /// 
        /// 
        public ActionResult Example1()
        {
            return View();
        }

Example 2:

 This example you can return the view with View Name (Which is already exists). So you can use the single view in multiple action controller methods based on you requirement.

So, This example you are using Example1 view for the action controller method Example2.

Code:
        
       /// 
        /// Returns the View as you mentioned in the parameter
        /// named as Example1.cshtml
        /// 
        /// 
        public ActionResult Example2()
        {
            return View("Example1");
        }

Example 3 :

 This example, you can return the view with the View Name(Which is already exists) and also with the model entity.

Code:

Model Entity:
       namespace Mvc.ViewResult.Models
       {
          public class DemoModel
          {
              public string Name { get; set; }
          }
       }
Action Controller method returns as View with model entity
        /// 
        /// Returns the view Example1 with the DemoModel entity
        /// 
        /// 
        public ActionResult Example3()
        {
            var demoModel = new DemoModel
            {
                Name = "View Result example"
            };
            return View("Example1", demoModel);
        }

Example 4: 

This example, you can return the same view named as controller action method with the entity Demo Model.

Code:

        /// 
        /// Returns the view same as Controller method name 
        /// (i.e Example4.cshtml) with the DemoModel entity
        /// 
        /// 
        public ActionResult Example4()
        {
            var demoModel = new DemoModel
            {
                Name = "View Result example"
            };
            return View(demoModel);
        }

Example 5: 

This example, you can return the view named as Example1 (Already exists) and the second parameter as _Layout which acts as dynamic master page. you can assign the master page dynamically based on your code conditions or requirements.

Code:
        /// 
        /// Returns the View named as Example1 with the 
        /// dynamic master page (_Layout) as a second parameter
        /// 
        /// 
        public ActionResult Example5()
        {
            return View("Example1", "_Layout");
        }

Example 6:

This example, you can return the view named as Example1(already exists) , Master page named as (_Layout) and also the Demo Model entity.

Code:

        /// 
        /// Returns the view Example1, Master page _Layout
        /// with the Demo Model entity
        /// 
        /// 
        public ActionResult Example6()
        {
            var demoModel = new DemoModel
            {
                Name = "View Result example"
            };
            return View("Example1", "_Layout", demoModel);
        }

Conclusion:

I hope this article gives you brief idea on ViewResult Return Type with the code examples.
Please provide your valuable suggestions and comments if any.
 

1 comment: