/// <summary> /// 从字符串解析为Action /// </summary> /// <param name="helper"></param> /// <param name="keyValues"></param> /// <returns></returns> public static IHtmlContent ParseAction(this IHtmlHelper helper, string keyValues) { var datas = keyValues.Matches(@"\w+=\w+").Select(item => item.Split('=')).ToDictionary(kv => kv.First(), kv => (object)kv.Last(), StringComparer.OrdinalIgnoreCase); var action = TakeParameter(datas, "action"); var controller = TakeParameter(datas, "controller"); return(helper.Action(action, controller, new RouteValueDictionary(datas))); }
public static IHtmlContent Action(this IHtmlHelper helper, string action, string controller, object?[]?parameters = null) { if (helper is null) { throw new ArgumentNullException(nameof(helper)); } string area = (string)helper.ViewContext.RouteData.Values["area"]; return(helper.Action(action, controller, area, parameters)); }
public void DefaultFormRenderer_GetDefault_ShouldCallGetElementRendererOnEachProperty() { var textBox = new TextBox { Name = "test-1" }; var stubWebpage = new StubWebpage { FormProperties = new List <FormProperty> { textBox } }; var formElementRenderer = A.Fake <IFormElementRenderer>(); A.CallTo(() => _elementRendererManager.GetElementRenderer <FormProperty>(textBox)) .Returns(formElementRenderer); A.CallTo(() => formElementRenderer.AppendElement(textBox, _existingValue, _siteSettings.FormRendererType)).Returns(new TagBuilder("input")); A.CallTo(() => _htmlHelper.Action("Render", "Form", A <object> ._)).Throws <Exception>(); _defaultFormRenderer.GetDefault(_htmlHelper, stubWebpage, new FormSubmittedStatus(false, null, _formCollection)); A.CallTo(() => _elementRendererManager.GetElementRenderer <FormProperty>(textBox)).MustHaveHappened(); }
public static IHtmlContent MetaRoute(this IHtmlHelper helper, Dictionary <string, object> routeValues, IMetaObject toRender) { if (routeValues is null) { throw new ArgumentNullException(nameof(routeValues)); } if (toRender is null) { throw new ArgumentNullException(nameof(toRender)); } string ControllerName = routeValues["controller"]?.ToString() ?? string.Empty; string ActionName = routeValues["action"]?.ToString() ?? string.Empty; string AreaName = routeValues["area"]?.ToString() ?? string.Empty; if (string.IsNullOrWhiteSpace(ControllerName)) { throw new ArgumentException("Controller name must be specified", nameof(routeValues)); } if (string.IsNullOrWhiteSpace(ActionName)) { throw new ArgumentException("Action name must be specified", nameof(routeValues)); } Type ControllerType = ControllerFactory.GetControllerType(ControllerName, routeValues["area"].ToString()); if (ControllerType is null) { throw new Exception($"Controller type not found for {ControllerName}"); } MethodInfo?m = ControllerType.GetMethod(ActionName); if (m is null) { return(new HtmlString($"Could not find method {ActionName} on controller {ControllerName}")); } ParameterInfo[] MethodParameters = m.GetParameters(); object?model = null; if (MethodParameters.Any()) { Type TargetParameter = MethodParameters.First().ParameterType; //If the action we're sending this too doesn't accept a MetaObject then it //Likely accepts the real value type the object was created from so we cast it back //If that doesn't work then theres something wrong with the way everything is tagged if (!TargetParameter.IsAssignableFrom(toRender.GetType())) { model = toRender.GetValue(TargetParameter); } else { model = toRender; } } return(helper.Action(ActionName, ControllerName, AreaName, new object?[] { model })); }