示例#1
0
        private static FormVm BuildFormVmFromAction(SirenDotNet.Action action)
        {
            var form = new FormVm
            {
                ActionUrl   = action.Href.ToString(),
                DisplayName = action.Title ?? action.Name ?? "link",
                Method      = action?.Method.ToString().ToLower(),
                Inputs      = action.Fields?.Select(field =>
                {
                    var propertyVm = new PropertyVm(typeof(string), field.Name)
                    {
                        DisplayName = field.Name
                    };
                    if (field.Type.ToString() == "select")
                    {
                        propertyVm.Choices = ((IEnumerable <JObject>)field.ExtensionData["options"])
                                             .Select(jo => Tuple.Create(jo["name"].Value <string>(), jo["value"].Value <string>()));
                    }
                    return(propertyVm);
                })?.ToArray() ?? Enumerable.Empty <PropertyVm>().ToArray()
            };



            if (form.Method != "get" && form.Method != "post")
            {
                form.ActionUrl += form.ActionUrl.Contains("?") ? "&" : "?";
                form.ActionUrl += "_method=" + form.Method.ToString().ToUpper();
                form.Method     = "post";
            }
            return(form);
        }
示例#2
0
        public ActionResult Create(FormVm model)
        {
            model.IsNew = model.Id == null;

            if (!ModelState.IsValid)
            {
                return(View());
            }
            if (model.IsNew)
            {
                _db.Departments.Add(new Department
                {
                    Name        = model.Name,
                    CreatedBy   = Auth.User.Name,
                    CreatedTime = DateTime.UtcNow.AddHours(2)
                });
            }
            else
            {
                var dep = _db.Departments.SingleOrDefault(x => x.Id == model.Id);
                if (dep == null)
                {
                    HttpNotFound();
                }
                dep.Name         = model.Name;
                dep.ModifiedTime = DateTime.UtcNow.AddHours(2);
            }
            _db.SaveChanges();
            return(RedirectToAction("Index", "Departments"));
        }
示例#3
0
        public static async Task <string> RenderAsync(this FormVm formVm)
        {
            using (var serviceScope = RazorContextBuilder.ServiceScopeFactory.Value.CreateScope())
            {
                var helper = serviceScope.ServiceProvider.GetRequiredService <RazorViewToStringRenderer>();

                return(await helper.RenderViewToStringAsync("Views/Shared/FormFactory/Form.cshtml", formVm));
            }
        }
        public ActionResult Index(string theName)
        {
            if (!string.IsNullOrEmpty(theName))
            {
                var sharedclass = new SharedClass();
                var result      = sharedclass.SayMyName(theName);
                var model       = new FormVm
                {
                    Result = result
                };

                return(View(model));
            }

            return(View());
        }
示例#5
0
        public async Task TestForm()
        {
            var form = new FormVm
            {
                ActionUrl   = "http://example.org/foo",
                DisplayName = "somelink",
                Method      = "GET",
                Inputs      = new List <PropertyVm>()
            };

            form.Inputs.Add(new PropertyVm(typeof(string), "_method")
            {
                DisplayName = "HttpMethod",
                Choices     = new[] { "get", "put", "post", "delete" }
            });
            var html     = form.Render(new MyFfHtmlHelper()).ToString();
            var actualCq = CQ.CreateFragment("<div>" + html.ToString() + "</div>");

            actualCq.Find("form").Single();
        }
        private static FormVm BuildFormVmFromAction(Action action)
        {
            var form = new FormVm
            {
                ActionUrl   = action.Href.ToString(),
                DisplayName = action.Title ?? action.Name ?? "link",
                Method      = action?.Method.ToString().ToLower(),
                Inputs      =
                    action.Fields?.Select(field => new PropertyVm(typeof(string), field.Name)
                {
                    DisplayName = field.Name
                })
                    ?.ToArray() ?? Enumerable.Empty <PropertyVm>(),
            };

            if (form.Method != "get" && form.Method != "post")
            {
                form.ActionUrl += form.ActionUrl.Contains("?") ? "&" : "?";
                form.ActionUrl += "_method=" + form.Method.ToString().ToUpper();
                form.Method     = "post";
            }
            return(form);
        }
 public static IHtmlContent Render(this FormVm formVm, IHtmlHelper html)
 {
     return(html.Partial("FormFactory/Form", formVm));
 }
示例#8
0
 public NavbarForm()
 {
     Form = new FormVm();
 }
示例#9
0
 public static RawString Render(this FormVm formVm, RazorTemplateHtmlHelper html)
 {
     return(html.Partial("FormFactory/Form", formVm));
 }