Understand the View Handling Methods in Side Controllers


As many of us know we used observer pattern to make get an intimation and to give an intimation of any changes across layers in classic MVC. Asp.net MVC controllers have built-in methods which will be fired like constructors during application flow and we need to handle these methods for a better co-ordination between Model-View-Controller. This article mainly focused methods which are handling inside controller layer.

Open a default MVC3 internet application and run it. Make sure that you are getting below screen on running it.

MVC application

This is the default view named "Index" showing here. In side solution explorer, you can see files belongs to this "Index" view like below screen shot

MVC application classes

As per above image the "Index" view controller name is "HomeController", but how? You can see in side "HomeController" the line meant o return the "Index" view as below. So this the controller meant to "Index" view

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}


The above method with the same name of the view will be executed on application load. Once you put a break point there, you will get it. So this is the default scenario and returns simply the view after updated the ViewBag. Now the question is that what will do if we need more checking, especially some model level checking in side this method, then only returns the view? Answer is overloading. We have built-in overloading methods on top of default once like below.

public ActionResult Index(<<Model>>)
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}


So we need to use a model for overriding default. Is there any model attached to "Index" view? How we know that? Open LogOn.cshtml and inside locate below line as model binding for this LogOnModel.

@model MvcApplication1.Models.LogOnModel
@{
    ViewBag.Title = "Log On";
}

We haven't anything like this inside Index.cshtml. So write the binding by your own. Please note that we are only writing the bindings. The real model residing in side AccountModels.cs file where you can write new models, if need. In this case we are using existing LogOnModel like below

@model MvcApplication1.Models.LogOnModel
@{
    ViewBag.Title = "Home Page";
}


Now you have a model binded to "Index" view. So rewrite your default view handling method like below. Running application will produce same result and you can see this new method is firing as expected.

public ActionResult Index(LogOnModel model)
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}

So where practically we can use this? Ok, I created an extension method of HtmlHelper. See here for details http://www.c-sharpcorner.com/UploadFile/jaishmathews/8652/, for checking specific domain for users. Add a new property to the existing LogOnModel like below

MVC classes

Our extension method will use above model property as

public static string CheckDomain(this HtmlHelper html, LogOnModel model)
{
    if (model.Domain.Equals("Something"))
    {
        //Some operation
    }
    return string.Empty; ;
}

You will call this extension method from your "Index" view as

@
using MvcApplication1.Helpers;
@model MvcApplication1.Models.
LogOnModel
@{
    ViewBag.Title =
"Home Page";
}
@Html.CheckDomain(Model)

Alert : -

Put a break point in above method and you will surprise to see the model is empty

MVC classes

These are small but important tips you need to be focused. The culprits is our new overloaded method written

public ActionResult Index(LogOnModel model)
{
    ViewBag.Message =
"Welcome to ASP.NET MVC!";
    return View();

This method inside the control taking one model and just returning the view without telling about the model what it received. It should tell like below

public ActionResult Index(LogOnModel model)
{
    ViewBag.Message =
"Welcome to ASP.NET MVC!";
    return View(model);

Now you can see the model loaded as

classes in MVC application

I am not extending this by putting more, but a small exercise for you. We have another overload method available with 2 parameters.

    public ActionResult SomeView(<<model>>,<<url>>)

Just check where we can use this. It's a core overloaded method


Similar Articles