示例#1
0
        public ActionResult Save(string org, string service, string edition, string id, string html)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(BadRequest("Id is required"));
            }

            if (string.IsNullOrWhiteSpace(html))
            {
                html = "<form class=\"droppable sortable clearfix control-target ui-droppable ui-sortable\">";
                html = html.Replace(html, "<form asp-controller=\"instance\" asp-action=\"edit\" class=\"droppable sortable clearfix control-target ui-droppable ui-sortable\"></form>");
            }
            else
            {
                html = html.Replace(
                    "<form class=\"droppable sortable clearfix control-target ui-droppable ui-sortable\">",
                    "<form asp-controller=\"instance\" asp-action=\"edit\" class=\"droppable sortable clearfix control-target ui-droppable ui-sortable\">");
            }

            _viewRepository.SaveView(org, service, edition, id, html);

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(html);

            foreach (HtmlNode node in doc.DocumentNode.Descendants().Where(r => r.Attributes.Any(x => x.Name == "altinn-group-binding")))
            {
                string target         = node.GetAttributeValue("altinn-group-binding", string.Empty);
                string indexName      = node.GetAttributeValue("altinn-group-index-name", string.Empty);
                string indexParamName = node.GetAttributeValue("altinn-group-index-param-name", string.Empty);

                if (string.IsNullOrEmpty(indexName))
                {
                    string indexParamVariable =
                        "@{ int " + indexParamName + " = 0; try { " + indexParamName + " = Convert.ToInt32(ViewBag.RequestContext.Params[\"" + indexParamName + "\"]); } catch { } }";
                    node.InnerHtml = indexParamVariable + node.InnerHtml;
                    if (!string.IsNullOrEmpty(target))
                    {
                        //throw new Exception();
                    }
                }
                else
                {
                    string forLoop = "@for (int " + indexName + " = 0; " + indexName + " %LT% Model." + target + ".Count; " + indexName + "++) {";
                    node.InnerHtml = forLoop + node.InnerHtml + " } ";
                    if (!string.IsNullOrEmpty(target))
                    {
                        //throw new Exception();
                    }
                }
            }

            // Find HtmlNodes with resource texts
            foreach (HtmlNode htmlNode in doc.DocumentNode.Descendants().Where(n => n.Attributes.Any(x => x.Name == "altinn-text")))
            {
                if (!htmlNode.InnerHtml.Contains("altinn-text-template"))
                {
                    htmlNode.InnerHtml = "";
                }
            }

            ServiceMetadata metadata = _repository.GetServiceMetaData(org, service, edition);
            string          rootName = metadata.Elements.Values.First(e => e.ParentElement == null).TypeName;

            _viewRepository.SaveView(
                org,
                service,
                edition,
                id + "final",
                "@model " + string.Format(CodeGeneration.ServiceNamespaceTemplate, org, service, edition) + "." + rootName + "\n\n" + doc.DocumentNode.InnerHtml.Replace(" %LT%", "<"));

            return(Ok());
        }