Ejemplo n.º 1
0
        public ActionResult AddInstance(string name, string pattern, string[] fieldNames, string[] fieldValues)
        {
            ControllerInfo cInfo = SiteStructure.Current.Controllers.FirstOrDefault(ci => ci.Name == name);

            if (cInfo == null)
            {
                return(new HttpStatusCodeResult(500, "No such template: " + name));
            }

            try
            {
                int patternIdx = int.Parse(pattern);
                if (fieldNames == null)
                {
                    fieldNames = new string[0];
                }
                if (fieldValues == null)
                {
                    fieldValues = new string[0];
                }
                RouteValueDictionary rvs = new RouteValueDictionary();
                rvs.Add("controller", name);
                rvs.Add("action", cInfo.GetPatternAction(patternIdx));
                for (int i = 0; i < fieldNames.Length; i++)
                {
                    rvs.Add(fieldNames[i], fieldValues[i]);
                }
                foreach (var kvp in cInfo.GetPatternByDisplayIdx(patternIdx).Defaults)
                {
                    if (!rvs.ContainsKey(kvp.Key))
                    {
                        rvs.Add(kvp.Key, kvp.Value);
                    }
                }
                bool created = cInfo.CreateInstance(rvs);

                if (created)
                {
                    return(Content("OK"));
                }
                else
                {
                    return(new HttpStatusCodeResult(500, "Already exists"));
                }
            }
            catch (Exception ex)
            {
                return(new HttpStatusCodeResult(500, ex.Message));
            }
        }