Exemple #1
0
        private static RedirectToRouteResult RedirectToActionWithParameters <TController>(string actionName, object[] parameters, RouteValueDictionary otherValuesToMerge = null)
            where TController : Controller
        {
            Type controllerType  = typeof(TController);
            int  parametersCount = parameters.Count();

            var rvd = new RouteValueDictionary();

            rvd.Add(MvcConstants.ControllerRouteValueKey, MvcExpressionHelper.GetControllerName(controllerType));

            MethodInfo method = controllerType.GetMethods().SingleOrDefault(m => m.Name == actionName && !Attribute.IsDefined(m, HttpPostAttributeType) && m.GetParameters().Count() == parametersCount);

            Requires <ArgumentNullException> .IsNotNull(method);

            rvd.Add(MvcConstants.ActionRouteValueKey, method.Name);

            ParameterInfo[] methodParameters = method.GetParameters();
            for (int i = 0; i < parametersCount; ++i)
            {
                rvd.Add(methodParameters[i].Name, parameters[i]);
            }

            if (otherValuesToMerge != null)
            {
                rvd = rvd.Merge(otherValuesToMerge);
            }

            return(new RedirectToRouteResult(rvd));
        }
Exemple #2
0
        /*
         * public static RedirectToRouteResult RedirectToAction<T>(this Controller controller, Expression<Action<T>> action, object values)
         *  where T : Controller
         * {
         *  return RedirectToActionInternal<T>(action, new RouteValueDictionary(values));
         * }
         *
         * public static RedirectToRouteResult RedirectToAction<T>(this T controller, Expression<Action<T>> action, RouteValueDictionary values)
         *  where T : Controller
         * {
         *  return RedirectToActionInternal<T>(action, values);
         * }
         *
         * public static RedirectToRouteResult RedirectToAction<T>(this Controller controller, Expression<Action<T>> action, RouteValueDictionary values)
         *  where T : Controller
         * {
         *  return RedirectToActionInternal<T>(action, values);
         * }
         */

        #region Helpers

        private static RedirectToRouteResult RedirectToActionInternal <T>(Expression <Action <T> > action, RouteValueDictionary values) where T : Controller
        {
            RouteValueDictionary parameters = MvcExpressionHelper.GetRouteValuesFromExpression(action)
                                              .Merge(values);

            return(new RedirectToRouteResult(parameters));
        }
Exemple #3
0
        public static MvcHtmlString Action <TController>(this UrlHelper urlHelper, Expression <Action <TController> > action) where TController : Controller
        {
            Requires <ArgumentNullException> .IsNotNull(action);

            var newRouteValues = MvcExpressionHelper.GetRouteValuesFromExpression(action);
            var result         = MvcHtmlString.Create(urlHelper.RouteUrl(newRouteValues));

            return(result);
        }
Exemple #4
0
        public static string RouteUrl <TController>(this UrlHelper urlHelper, Expression <Action <TController> > action, object routeValues) where TController : Controller
        {
            Requires <ArgumentNullException> .IsNotNull(action);

            RouteValueDictionary newRouteValues = MvcExpressionHelper.GetRouteValuesFromExpression(action)
                                                  .Merge(new RouteValueDictionary(routeValues));

            return(urlHelper.RouteUrl(newRouteValues));
        }
Exemple #5
0
        public static RouteValueDictionary GetRouteValuesFrom <TController>(Expression <Action <TController> > editLinkAction, int id)
            where TController : Controller
        {
            var routeValues = MvcExpressionHelper.GetRouteValuesFromExpression(editLinkAction);
            var idEntry     = routeValues.First(entry => entry.Key.ToUpper().EndsWith("ID"));

            routeValues.Remove(idEntry.Key);
            routeValues.Add(idEntry.Key, id);
            return(routeValues);
        }
        public static MvcHtmlString ActionLink <TController>(this HtmlHelper helper, string linkText, Expression <Action <TController> > action, object routeValues, object htmlAttributes)
            where TController : Controller
        {
            Requires <ArgumentNullException> .IsNotNull(action);

            Requires <ArgumentNullException> .IsNotNull(linkText);

            Requires <ArgumentException> .GreaterThan(linkText.Length, 0);

            var routingValues = MvcExpressionHelper.GetRouteValuesFromExpression(action)
                                .Merge(new RouteValueDictionary(routeValues));

            return(helper.RouteLink(linkText, routingValues, new RouteValueDictionary(htmlAttributes)));
        }
        public static MvcHtmlString ActionImage <TController>(this HtmlHelper htmlHelper, Expression <Action <TController> > action, string relativeUrl, string altText, object routeValues, object htmlAttributes)
            where TController : Controller
        {
            Requires <ArgumentNullException> .IsNotNull(action);

            Requires <ArgumentNullException> .IsNotNull(relativeUrl);

            Requires <ArgumentException> .GreaterThan(relativeUrl.Length, 0);

            var routeValueDictionary = MvcExpressionHelper.GetRouteValuesFromExpression(action)
                                       .Merge(new RouteValueDictionary(routeValues));
            var actionName     = routeValueDictionary[MvcConstants.ActionRouteValueKey].ToString();
            var controllerName = routeValueDictionary[MvcConstants.ControllerRouteValueKey].ToString();

            return(htmlHelper.ViewContext.ImageLink(actionName, controllerName, routeValueDictionary, relativeUrl, altText, htmlAttributes));
        }
        public static MvcHtmlString DeleteButtonForm <TController>(this HtmlHelper helper, Expression <Action <TController> > action, object routeValues)
            where TController : Controller
        {
            var routingValues = MvcExpressionHelper.GetRouteValuesFromExpression(action);
            var formAction    = UrlHelper.GenerateUrl(null,
                                                      routingValues[ActionRouteValueKey].ToString(),
                                                      routingValues[ControllerRouteValueKey].ToString(),
                                                      new RouteValueDictionary(routeValues),
                                                      helper.RouteCollection,
                                                      helper.ViewContext.RequestContext,
                                                      true);
            var form = Tag.Form;

            form.Class(ConfirmDelete);
            form.MergeAttribute(FluentTagBuilder.Method, "post")
            .MergeAttribute(FluentTagBuilder.Action, formAction);
            form.Html(Tag.P.Html(helper.DeleteButton()));

            return(form.ToMvcHtmlString());
        }
        public static MvcForm BeginForm <TController>(this HtmlHelper helper, Expression <Action <TController> > action) where TController : Controller
        {
            var routeValues = MvcExpressionHelper.GetRouteValuesFromExpression(action);

            return(helper.BeginForm(routeValues));
        }