10

I have just started using MVC4 and the first action method that I look at has something new. I checked out the internet and can't find anything about this:

public ActionResult LogOn()
        {
            return ContextDependentView();
        }

Does anyone know what a ContextDependentView is ?

Something new to me.

Charles
  • 50,943
  • 13
  • 104
  • 142
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

1 Answers1

11

It's purpose is to facilitate View or PartialView action results for the Login and Register actions.

    private ActionResult ContextDependentView()
    {
        string actionName = ControllerContext.RouteData.GetRequiredString("action");
        if (Request.QueryString["content"] != null)
        {
            ViewBag.FormAction = "Json" + actionName;
            return PartialView();
        }
        else
        {
            ViewBag.FormAction = actionName;
            return View();
        }
    }

Like other things in MVC it is done by convention... the convention here is when the Request.QueryString contains a ?content=xxxx, it prepends "Json" to the action name, stuffs it a ViewBag property and returns the partial version of the View. For example:

A request for /Account/Login?content=test would be resolved to ViewBag.FormAction = "JsonLogin"; and then return a partial.

A request for /Account/Login has no content query string so its form action remains ViewBag.FormAction = "Login";

one.beat.consumer
  • 9,414
  • 11
  • 55
  • 98
  • 1
    Do you think this should really be a private method in this controller? Seems to me like it is something that could be used for any controller. Have you used this method yourself and did you leave it as private? – Samantha J T Star Feb 02 '12 at 06:16
  • To be honest, I've only begun to play with MVC4. I would bet this method will change before it goes to production. You could also move this into your own `BaseController : Controller` class if you like or find use for it. The trick in putting it in a base controller private or static, seems to be that it obligates you to make views for both partial and complete scenarios. I would leave it as it is and experiment with its function and see where it goes in Release Candidate (RC). – one.beat.consumer Feb 02 '12 at 07:01