pavanarya – Pavan Kumar Aryasomayajulu <br> My New Blog http://xyzcoder.com

Contact Email : pavan.aryasomayajulu@gmail.com

Rendering a View from a Controller in MVC3–Part 2

with one comment

Hi,
This is a continuation of my previous post that deals with model,view and render.Please read that before reading this url..

So Is It Compulsory to have a view in the shared folder or folder with controller name inside view folder?? :

1.The answer to this question is absolutely no. We can have view at any custom location and we can render them using controller.

public ActionResult Index()
        {
            
            return View("~/TestView.cshtml");
        }

Here we are trying to render a view that is not present in views folder . So when ever we are trying a render a view that is not present inside shared folder or the folder that has same name as a controller inside Views folder we are supposed to give the relative path of the view in the function return View().


2. Also it is not Compulsory to have the view name same as controller name. We can render a view with any name and we don’t have any restrictions.

Rendering a view that has a name which is different controller name:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class FirstController : Controller
    {
        //
        // GET: /First/
        public ActionResult Index()
        {
            Models.MyModel mod = new Models.MyModel();
            mod.firstname = "pavan";
            mod.lastname = "arya";
            return View("myFirstView", mod);
        }
    }
}

So we are trying to render a view named as myFirstView. Also i am passing MyModel object which is used to render the view.

Note: We are making use of model class MyModel. Please refer to my previous post for the Model class MyModel.cs.

myFirstView.cshtml I am adding this file in the following location Views\Shared\myFirstView.cshtml

@{
    //Layout = null;
}
@model MvcApplication2.Models.MyModel

    <div>
@{
    @Model.firstname;
    }
    </div>
  @using( Html.BeginForm() )
{
    @Html.TextBox("textField", (string)ViewBag.TextMessage)
     <input type="submit" value="Send" />
}

Let us explore this view

1.@Html
This is used to create html elements in the razor view.

2.@using(Html.BeginForm())

This is similar to tag in html or aspx

The content present inside this comes under form tag

So in the above razor view we are having a text box and button

How do we post data back

In simple we forms suppose if we have a button inside an aspx page. In the aspx.cs file we can have button click event handler that contains the code that will be executed when a button is clicked.

But we don’t have button click event handler inside the controller that can handle the button click events.

So how do we handle them??

Let us see the below controller that handles post backs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class FirstController : Controller
    {
        //
        // GET: /First/
        public string middlename;
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(FormCollection collection)
        {
            Models.MyModel mod = new Models.MyModel();
            mod.firstname = collection["textField"].ToString();
            mod.lastname = "arya";
            return View("~/Views/WebForm1.aspx", mod);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            Models.MyModel mod = new Models.MyModel();
            mod.firstname = "pavan";
            mod.lastname = "arya";
            return View("myFirstView", mod);
        }
    }
}

Why do we have two methods with same name inside the controller

We can see different attributes like

1.[AcceptVerbs(HttpVerbs.Get)]
when ever a request of this type (http://localhost:1427/First/Index/33) comes. This will execute First Controller and inside that Index function is invoked and 33 is the parameter for that index function.
So which index function is going to be executed
Simply the index method that has [AcceptVerbs(HttpVerbs.Get)] attribute.

Also If we have single method then it is not required to have this attribute.

2.[AcceptVerbs(HttpVerbs.Post)]—-And a parameter FormColection

If we can see the myFirstView.cshtml we are having a html form and a button inside that. So when we click on the button data is supposed to be post backed. So how do we handle that.

In order to handle the post back data we are supposed to have another method with the same name of the method that is used to render the view. Also we should have different parameters than the original method.
So in my example i am using a parameter called FormCollection. Formcollection contains all the information related to the form that is postbacked.

In addition to that we are supposed to have an extra attribute [AcceptVerbs(HttpVerbs.Post)].

Note: If we have a method that has [AcceptVerbs(HttpVerbs.Post)] but we dont have an overloded method for this method that has [AcceptVerbs(HttpVerbs.Get)] attribute then the view will not be generated. That means method declared with [AcceptVerbs(HttpVerbs.Post)] attribute is always used to handle post backs.

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(FormCollection collection)
        {
            Models.MyModel mod = new Models.MyModel();
            mod.firstname = collection["textField"].ToString();
            mod.lastname = "arya";
            return View("~/Views/WebForm1.aspx", mod);
        }

So the above method is used to handle the post back data from myFirstView.cshtml generated from Index().

We can retrieve form related data using FormCollection. It is a collection that contains element id as key and Corresponding element value as Collection value.
We are retrieving txtField textbox value like this collection[“textField”].ToString();

Finally we are rendering another view called WebForm1.aspx which is of type web form engine.

Thanks,
Pavan

Written by pavanarya

June 9, 2012 at 8:13 pm

Posted in MVC

One Response

Subscribe to comments with RSS.

  1. […] Posted in Jquery, MVC « Rendering a View from a Controller in MVC3–Part 2 […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: