# Creating Dynamic Forms from a JSON File

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Form</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2023.3.1114 version</td>
 </tr>
</table>

## Description

How can I generate dynamic Telerik UI for ASP.NET MVC Forms through a JSON file?

## Solution

1. Create the JSON file with the following structure and save it into the application.

   ```JS formData.json
       [{
           "Id": 16014,
           "Properties": [
               {
                   "Property": "Name",
                   "EditorType": "TextBox"
               },
               {
                   "Property": "StartDate",
                   "EditorType": "DatePicker"
               },
               {
                   "Property": "IsActive",
                   "EditorType": "CheckBox"
               },
               {
                   "Property": "Country",
                   "EditorType": "DropDownList"
               }
           ]
       },
       {
           "Id": 18231,
           "Properties": [
               {
                   "Property": "Name",
                   "EditorType": "TextBox"
               },
               {
                   "Property": "IsActive",
                   "EditorType": "CheckBox"
               }
           ]
       }]
   ```

2. Create the following Models:
   * <b>FormViewModel</b>—The Model that defines the properties and structure of the JSON data.

     ```C# FormViewModel
         public class FormViewModel
         {
             public int Id { get; set; } // The unique Form Id.
             public List<FormItems> Properties { get; set; } // The properties that will be used to populate the Form items.
         }
     ```
   * <b>FormItems</b>—The Model that represents the Form item.

     ```C# FormItems
         public class FormItems
         {
             public string Property { get; set; } // The name of the property that will bind to each Form item.
             public string EditorType { get; set; } // The name of the editor.
         }
     ```
   * <b>UserViewModel</b>—The Model to which each Form will bind.

     ```C# UserViewModel
         public class UserViewModel
         {
             public string Name { get; set; }
             public DateTime StartDate { get; set; }
             public bool IsActive { get; set; }
             public int Country { get; set; }
         }
     ```

3. Use the [`JsonConvert.DeserializeObject<T>()`](https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm) method to convert the JSON content from the file into a `List<FormViewModel>` objects.

   ```C# HomeController.cs
       using Newtonsoft.Json;

       public ActionResult Index()
       {
           var formDataitems = PopulateFormItems();
           return View(formDataitems);
       }

       private List<FormViewModel> PopulateFormItems()
       {
           var path = Path.Combine("wwwroot/formData.json");
           var items = new List<FormViewModel>();
           items = JsonConvert.DeserializeObject<List<FormViewModel>>(System.IO.File.ReadAllText(path));
           return items;
       }
   ```

4. Access the Model collection in the View and loop through each <b>FormViewModel</b> record to generate the respective Forms. Within the Form `Items()` configuration, loop through the nested <b>Properties</b> collection and define the editor of each item based on the value of the <b>EditorType</b> property.

   ```Razor Index.cshtml_HtmlHelper
       @model List<FormViewModel>

       @foreach (var item in Model)
       {
           @(Html.Kendo().Form<UserViewModel>()
           .Name("form_" + item.Id)
           .HtmlAttributes(new { action = "/Home/SaveForm", method = "POST" })
           .FormData(new UserViewModel())
           .Orientation("horizontal")
           .Validatable(v =>
           {
               v.ValidateOnBlur(true);
               v.ValidationSummary(vs => vs.Enable(false));
           })
           .Items(i =>
           {
               foreach (var prop in item.Properties)
               {
                   if (prop.EditorType == "DatePicker")
                   {
                       i.Add()
                       .Field(prop.Property)
                       .Label(l => l.Text(prop.Property))
                       .Editor(e => e.DatePicker());
                   }
                   else if (prop.EditorType == "DropDownList")
                   {
                       i.Add()
                       .Field(prop.Property)
                       .Label(l => l.Text(prop.Property))
                       .Editor(e =>
                       {
                           e.DropDownList()
                           .DataTextField("Text")
                           .DataValueField("Value")
                           .BindTo(new List<SelectListItem>() {
                               new SelectListItem() {
                                   Text = "Country 1",
                                   Value = "1"
                               },
                               new SelectListItem() {
                                   Text = "Country 2",
                                   Value = "2"
                               },
                               new SelectListItem() {
                                   Text = "Country 3",
                                   Value = "3"
                               }
                           });
                       });
                   }
                   else if (prop.EditorType == "CheckBox")
                   {
                       i.Add()
                       .Field(prop.Property)
                       .Label(l => l.Text(prop.Property))
                       .Editor(e => e.CheckBox());
                   }
                   else
                   {
                       i.Add()
                       .Field(prop.Property)
                       .Label(l => l.Text(prop.Property))
                       .Editor(e => e.TextBox());
                   }
               }
           })
           )
       }
   ```

## More ASP.NET MVC Form Resources

* [ASP.NET MVC Form Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/layout/form/overview)

* [ASP.NET MVC Form Demos](https://demos.telerik.com/aspnet-mvc/form)

* [ASP.NET MVC Form Product Page](https://www.telerik.com/aspnet-mvc/form)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Form for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/form)

* [Server-Side API Reference of the Form for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/form)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
