Wednesday, 26 December 2012

Using view as both View and Partial View


A couple of days back, we were in a situation where we have to display the same view (say "AddTicket") as
  • View in some cases(new tab) and
  • as partial view in other(in modal pop-up).
To accomplish that, there might be various approaches but two thoughts came in my mind-
  • Create separate view and partial view (which was discarded as we would have to write the same HTML layout twice).
  • Create a Partial View and use it as View wherever and whenever required.

To do that, first create a partial view with Layout property set to "null".(Obviously)

public ActionResult AddTicket()
        {
            /*
             We are using the same as View and Partial View here.
             */
            if (Request.IsAjaxRequest()|| ControllerContext.IsChildAction)
            {
                return PartialView("PartialViewName");
            }
            else
            {

               //Set the Layout for the partial view
                return View("PartialViewName","~/Views/Shared/_Layout.cshtml");
            }
        }

Needless to say, i am expecting there should be a better approach than this. Looking forward for the suggestions.

Thanks,
S. Shafique
Fok at RnD Team member

No comments:

Post a Comment