/// <summary> /// Writes an opening <form> tag to the response. When the user submits the form, the request will be processed by an actionName method. /// </summary> /// <param name="htmlHelper">The HTML helper instance that this method extends.</param> /// <param name="model">The model to built form for.</param> /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param> /// <returns>An opening <form> tag. </returns> public static MvcForm BeginResourceForm <TModel>(this HtmlHelper <TModel> htmlHelper, IDictionary <string, object> htmlAttributes) where TModel : IEntity { RouteNames routeNames = htmlHelper.ViewContext.HttpContext.Request.GetRouteNames(); UrlHelper url = new UrlHelper(htmlHelper.ViewContext.RequestContext); string actionName = htmlHelper.ViewContext.RouteData.GetRequiredString("action"); bool isNewAction = actionName.Equals(routeNames.NewName, StringComparison.InvariantCultureIgnoreCase); string targetActionName = isNewAction ? routeNames.CreateName : routeNames.UpdateName; string formName = htmlHelper.ViewData.Model.GetType().Name.ToLower(); string formId = string.Format("{0}_{1}", targetActionName, formName); string formAction = url.Action(targetActionName, htmlHelper.ViewContext.RequestContext.RouteData.Values); TagBuilder tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes <string, object>(htmlAttributes); tagBuilder.MergeAttribute("id", formId); tagBuilder.MergeAttribute("name", formName); tagBuilder.MergeAttribute("action", formAction); tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(FormMethod.Post)); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); if (!isNewAction) { htmlHelper.ViewContext.Writer.Write(htmlHelper.HttpMethodOverride(HttpVerbs.Put).ToHtmlString()); } htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); return(new MvcForm(htmlHelper.ViewContext)); }
private async void LoadRouteNames() { try { RouteNames.Clear(); if (Stop.Routes == null) { return; } IsBusy = true; foreach (var routeId in Stop.Routes) { //var route = await ApiLayer.GetTransitRoute(routeId, new System.Threading.CancellationToken()); var route = await DataSource.GetTransitRouteAsync(routeId, DataSourcePreference.All, LoadRoutesTokenSource.Token); if (route.HasData) { RouteNames.Add(route.Data.Name); } } RoutesLoaded = true; } catch (OperationCanceledException) { } finally { IsBusy = false; } }
public ResourcesMapping(RouteNames routeNames, ResourcesMapper resourcesMapper) { names = routeNames; ResourceName = ControllerName <TController>(); resourcesMapper.SetResourceAs(ResourceName); _resourcesMapper = resourcesMapper; }
public ResourceMapping(RouteNames names, ResourceMapper resourceMapper) { _names = names; ResourceName = ControllerName <TController>(); MappedName = Inflector.Singularize(ResourceName); resourceMapper.ResourceName = ResourceName; _resourceMapper = resourceMapper; }
private void EnsureIncludedActionsIsInitialized() { var configuration = new RouteNames(); if (IncludedActions == null) { IncludedActions = new List <string> { configuration.IndexName, configuration.ShowName, configuration.NewName, configuration.CreateName, configuration.EditName, configuration.UpdateName, configuration.DestroyName } } ; }
//protected bool IsNestedResource(int nestedId) { // // Get the RouteName // string currentRouteName = this.ControllerContext.RouteData.Values["RouteName"] as string; // if (currentRouteName != null) { // // We have to get the child category name // string url = RouteNames.GetRoute(currentRouteName); // string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString(); // string[] urlParts = url.Split('/'); // if (urlParts.Length == 3) { // string childResourceName = urlParts[2]; // string nestedRouteName = RouteNames.GetNestedRouteName(controllerName, childResourceName, Action.SHOW); // Url.Route(nestedRouteName, new {id = this.BaseRepository.ID}); // } // string actiona = this.ControllerContext.RouteData.Values["action"].ToString(); // // Get the route from the current route name // RouteNames.GetRoute() // } //} protected string GenerateNestedLink <T>(T instance, bool usePKForChildId = true) { string locationUri = string.Empty; string parent = this.ControllerContext.RouteData.Values["controller"].ToString(); string parentId = this.ControllerContext.RouteData.Values["id"].ToString(); string child = this.ControllerContext.RouteData.Values["action"].ToString(); string childIdName = string.Format("{0}Id", child); //string currentAction = this.ControllerContext.RouteData.Values["action"].ToString(); // Get the id of the newly created instance var type = typeof(T); PropertyInfo property = null; if (usePKForChildId) { property = type.GetProperty("id"); } else { property = type.GetProperty(childIdName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); } if (property != null) { var id = property.GetValue(instance, null); if (id != null) { Dictionary <string, object> routeValues = new Dictionary <string, object>(); routeValues.Add("controller", parent); routeValues.Add("id", parentId); routeValues.Add("action", child); routeValues.Add(childIdName, id); try { locationUri = Url.Route(RouteNames.GetNestedRouteName(parent, child, RouteAction.SHOW), routeValues); } catch { // Probably the route is not configured explicitly string action = string.Empty; locationUri = RouteConfiguration.GetNestedRouteUrl(parent, child, RouteAction.SHOW, out action); locationUri = locationUri.Replace("{id}", parentId).Replace("{" + childIdName + "}", id.ToString()); } } } return(locationUri); }
protected string GenerateLink <T>(T instance) { string locationUri = string.Empty; string currentController = this.ControllerContext.RouteData.Values["controller"].ToString(); //string currentAction = this.ControllerContext.RouteData.Values["action"].ToString(); // Get the id of the newly created instance var type = typeof(T); var property = type.GetProperty("id"); if (property != null) { var id = property.GetValue(instance, null); if (id != null) { locationUri = Url.Route(RouteNames.FindRouteName(RouteNames.DefaultResource, RouteAction.SHOW), new { controller = currentController, id = id.ToString() }); } } return(locationUri); }
protected string GenerateLink(int id) { string locationUri = string.Empty; string currentController = this.ControllerContext.RouteData.Values["controller"].ToString(); string action = this.ControllerContext.RouteData.Values["action"].ToString(); string parentId = this.ControllerContext.RouteData.Values["id"].ToString(); //action = RouteNames.Singularize(action); string route_name = RouteNames.GetNestedRouteName(currentController, action, RouteAction.SHOW); var name = ""; //string parentResourceIdName = currentController + "Id"; string parentResourceIdName = "id"; string childResourceIdName = action + "Id"; Dictionary <string, object> dict = new Dictionary <string, object>(); dict.Add(parentResourceIdName, parentId); dict.Add(childResourceIdName, id); //locationUri = Url.Route(route_name, // new { controller = currentController, id = id.ToString()}); locationUri = Url.Route(route_name, dict); return(locationUri); }
public static string Destroy(this UrlHelper urlHelper) { RouteNames routeNames = urlHelper.RequestContext.HttpContext.Request.GetRouteNames(); return(urlHelper.Action(routeNames.DestroyName)); }
public static string Delete(this UrlHelper urlHelper, IEntity model) { RouteNames routeNames = urlHelper.RequestContext.HttpContext.Request.GetRouteNames(); return(urlHelper.Action(routeNames.DeleteName, new { @id = model.Id })); }
private static DependencyConfig CreateDependencyConfig() { var routeNames = new RouteNames(); return(new DependencyConfig(routeNames)); }