/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Everest.Library.Mvc{
public class ActionResultHelper
{
Controller controller;
public Controller Controller
{
get
{
return controller;
}
}
public ActionResultHelper(Controller controller)
{
this.controller = controller;
}
#region Json
public JsonResult Json(object data)
{
return this.Json(data, null);
}
public JsonResult Json(object data, string contentType)
{
return this.Json(data, contentType, null);
}
public virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
{
return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding };
}
#endregion
#region PartialView
public PartialViewResult PartialView()
{
return this.PartialView(null, null);
}
public PartialViewResult PartialView(object model)
{
return this.PartialView(null, model);
}
public PartialViewResult PartialView(string viewName)
{
return this.PartialView(viewName, null);
}
public virtual PartialViewResult PartialView(string viewName, object model)
{
if (model != null)
{
controller.ViewData.Model = model;
}
return new PartialViewResult { ViewName = viewName, ViewData = Controller.ViewData, TempData = Controller.TempData };
}
#endregion
#region View
public ViewResult View()
{
return this.View(null, null, null);
}
public ViewResult View(object model)
{
return this.View(null, null, model);
}
public ViewResult View(string viewName)
{
return this.View(viewName, null, null);
}
public ViewResult View(IView view)
{
return this.View(view, null);
}
public ViewResult View(string viewName, object model)
{
return this.View(viewName, null, model);
}
public ViewResult View(string viewName, string masterName)
{
return this.View(viewName, masterName, null);
}
public virtual ViewResult View(IView view, object model)
{
if (model != null)
{
Controller.ViewData.Model = model;
}
return new ViewResult { View = view, ViewData = Controller.ViewData, TempData = Controller.TempData };
}
public virtual ViewResult View(string viewName, string masterName, object model)
{
if (model != null)
{
Controller.ViewData.Model = model;
}
return new ViewResult { ViewName = viewName, MasterName = masterName, ViewData = Controller.ViewData, TempData = Controller.TempData };
}
#endregion
#region Redirect
public virtual RedirectResult Redirect(string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException("url");
}
return new RedirectResult(url);
}
public RedirectToRouteResult RedirectToAction(string actionName)
{
return this.RedirectToAction(actionName, (RouteValueDictionary)null);
}
public RedirectToRouteResult RedirectToAction(string actionName, object values)
{
return this.RedirectToAction(actionName, new RouteValueDictionary(values));
}
public RedirectToRouteResult RedirectToAction(string actionName, string controllerName)
{
return this.RedirectToAction(actionName, controllerName, (RouteValueDictionary)null);
}
public RedirectToRouteResult RedirectToAction(string actionName, RouteValueDictionary values)
{
return this.RedirectToAction(actionName, null, values);
}
public RedirectToRouteResult RedirectToAction(string actionName, string controllerName, object values)
{
return this.RedirectToAction(actionName, controllerName, new RouteValueDictionary(values));
}
public virtual RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary values)
{
if (string.IsNullOrEmpty(actionName))
{
throw new ArgumentException( "actionName");
}
RouteValueDictionary dictionary = new RouteValueDictionary();
dictionary["action"] = actionName;
if (!string.IsNullOrEmpty(controllerName))
{
dictionary["controller"] = controllerName;
}
if ((!dictionary.ContainsKey("controller") && (Controller.RouteData != null)) && Controller.RouteData.Values.ContainsKey("controller"))
{
dictionary["controller"] = Controller.RouteData.Values["controller"];
}
if (values != null)
{
foreach (KeyValuePair<string, object> pair in values)
{
dictionary[pair.Key] = pair.Value;
}
}
return new RedirectToRouteResult(dictionary);
}
public RedirectToRouteResult RedirectToRoute(object values)
{
return this.RedirectToRoute(new RouteValueDictionary(values));
}
public RedirectToRouteResult RedirectToRoute(string routeName)
{
return this.RedirectToRoute(routeName, (RouteValueDictionary)null);
}
public RedirectToRouteResult RedirectToRoute(RouteValueDictionary values)
{
return this.RedirectToRoute(null, values);
}
public RedirectToRouteResult RedirectToRoute(string routeName, object values)
{
return this.RedirectToRoute(routeName, new RouteValueDictionary(values));
}
public virtual RedirectToRouteResult RedirectToRoute(string routeName, RouteValueDictionary values)
{
return new RedirectToRouteResult(routeName, (values != null) ? new RouteValueDictionary(values) : new RouteValueDictionary());
}
#endregion
}
}
|