Create dynamic forms that grow at run time












1














I'm working in asp.net core inside a MVC application. I'm using the scaffolding feature that creates the views and controller based on a model. Below is the model that i'm using:



class ShoppingList
{
public int ShoppingListId { get; set; }
public string Name { get; set; }
public List<string> ListItems { get; set; }
}


The form that displays to the user via the view only displays the field for Name. I would like the form to be able to show a field for a list item, and then if the user wants to add another list item they can hit a button to add another field to do so. They at run time decide how many shopping list items they want to add.



Here is the razor cshtml form i'm using:



<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>


Is there an easy way to do this? I don't want to have to hard code a number.










share|improve this question
























  • use jQuery to add an input element to the DOM.
    – Shyju
    Oct 20 '17 at 17:14










  • the issue I get with that is, when I submit the form the newly added elements don't get sent with the submit. @Shyju
    – conterio
    Oct 20 '17 at 17:15










  • it does as long us the names matches with what the model binder is looking for. Share the code what you tried
    – Shyju
    Oct 20 '17 at 17:17










  • @JeremyConterio if you are using Razor views I will have an answer for you.
    – Jared
    Oct 20 '17 at 17:17










  • @Jared I'm using razor pages. I'll updated my question to show the form i'm using.
    – conterio
    Oct 20 '17 at 17:22
















1














I'm working in asp.net core inside a MVC application. I'm using the scaffolding feature that creates the views and controller based on a model. Below is the model that i'm using:



class ShoppingList
{
public int ShoppingListId { get; set; }
public string Name { get; set; }
public List<string> ListItems { get; set; }
}


The form that displays to the user via the view only displays the field for Name. I would like the form to be able to show a field for a list item, and then if the user wants to add another list item they can hit a button to add another field to do so. They at run time decide how many shopping list items they want to add.



Here is the razor cshtml form i'm using:



<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>


Is there an easy way to do this? I don't want to have to hard code a number.










share|improve this question
























  • use jQuery to add an input element to the DOM.
    – Shyju
    Oct 20 '17 at 17:14










  • the issue I get with that is, when I submit the form the newly added elements don't get sent with the submit. @Shyju
    – conterio
    Oct 20 '17 at 17:15










  • it does as long us the names matches with what the model binder is looking for. Share the code what you tried
    – Shyju
    Oct 20 '17 at 17:17










  • @JeremyConterio if you are using Razor views I will have an answer for you.
    – Jared
    Oct 20 '17 at 17:17










  • @Jared I'm using razor pages. I'll updated my question to show the form i'm using.
    – conterio
    Oct 20 '17 at 17:22














1












1








1







I'm working in asp.net core inside a MVC application. I'm using the scaffolding feature that creates the views and controller based on a model. Below is the model that i'm using:



class ShoppingList
{
public int ShoppingListId { get; set; }
public string Name { get; set; }
public List<string> ListItems { get; set; }
}


The form that displays to the user via the view only displays the field for Name. I would like the form to be able to show a field for a list item, and then if the user wants to add another list item they can hit a button to add another field to do so. They at run time decide how many shopping list items they want to add.



Here is the razor cshtml form i'm using:



<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>


Is there an easy way to do this? I don't want to have to hard code a number.










share|improve this question















I'm working in asp.net core inside a MVC application. I'm using the scaffolding feature that creates the views and controller based on a model. Below is the model that i'm using:



class ShoppingList
{
public int ShoppingListId { get; set; }
public string Name { get; set; }
public List<string> ListItems { get; set; }
}


The form that displays to the user via the view only displays the field for Name. I would like the form to be able to show a field for a list item, and then if the user wants to add another list item they can hit a button to add another field to do so. They at run time decide how many shopping list items they want to add.



Here is the razor cshtml form i'm using:



<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>


Is there an easy way to do this? I don't want to have to hard code a number.







asp.net-mvc forms asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '17 at 17:24

























asked Oct 20 '17 at 17:11









conterio

4681314




4681314












  • use jQuery to add an input element to the DOM.
    – Shyju
    Oct 20 '17 at 17:14










  • the issue I get with that is, when I submit the form the newly added elements don't get sent with the submit. @Shyju
    – conterio
    Oct 20 '17 at 17:15










  • it does as long us the names matches with what the model binder is looking for. Share the code what you tried
    – Shyju
    Oct 20 '17 at 17:17










  • @JeremyConterio if you are using Razor views I will have an answer for you.
    – Jared
    Oct 20 '17 at 17:17










  • @Jared I'm using razor pages. I'll updated my question to show the form i'm using.
    – conterio
    Oct 20 '17 at 17:22


















  • use jQuery to add an input element to the DOM.
    – Shyju
    Oct 20 '17 at 17:14










  • the issue I get with that is, when I submit the form the newly added elements don't get sent with the submit. @Shyju
    – conterio
    Oct 20 '17 at 17:15










  • it does as long us the names matches with what the model binder is looking for. Share the code what you tried
    – Shyju
    Oct 20 '17 at 17:17










  • @JeremyConterio if you are using Razor views I will have an answer for you.
    – Jared
    Oct 20 '17 at 17:17










  • @Jared I'm using razor pages. I'll updated my question to show the form i'm using.
    – conterio
    Oct 20 '17 at 17:22
















use jQuery to add an input element to the DOM.
– Shyju
Oct 20 '17 at 17:14




use jQuery to add an input element to the DOM.
– Shyju
Oct 20 '17 at 17:14












the issue I get with that is, when I submit the form the newly added elements don't get sent with the submit. @Shyju
– conterio
Oct 20 '17 at 17:15




the issue I get with that is, when I submit the form the newly added elements don't get sent with the submit. @Shyju
– conterio
Oct 20 '17 at 17:15












it does as long us the names matches with what the model binder is looking for. Share the code what you tried
– Shyju
Oct 20 '17 at 17:17




it does as long us the names matches with what the model binder is looking for. Share the code what you tried
– Shyju
Oct 20 '17 at 17:17












@JeremyConterio if you are using Razor views I will have an answer for you.
– Jared
Oct 20 '17 at 17:17




@JeremyConterio if you are using Razor views I will have an answer for you.
– Jared
Oct 20 '17 at 17:17












@Jared I'm using razor pages. I'll updated my question to show the form i'm using.
– conterio
Oct 20 '17 at 17:22




@Jared I'm using razor pages. I'll updated my question to show the form i'm using.
– conterio
Oct 20 '17 at 17:22












2 Answers
2






active

oldest

votes


















5














If you want to allow the user to add a new form element on the client side you need to use javascript to update the DOM with the new element you want to add. To list the existing items you may use editor templates. Mixing these 2 will give you a dynamic form. The below is a basic implementation.



To use editor templates, we need to create an editor template for the property type. I would not do that for string type which is more like a generic one. I would create a custom class to represent the list item.



public class Item
{
public string Name { set; get; }
}
public class ShoppingList
{
public int ShoppingListId { get; set; }
public string Name { get; set; }
public List<Item> ListItems { get; set; }

public ShoppingList()
{
this.ListItems=new List<Item>();
}
}


Now, Create a directory called EditorTemplates under ~/Views/YourControllerName or ~/Views/Shared/ and create a view called Item.cshtml which will have the below code



@model  YourNameSpaceHere.Item
<input type="text" asp-for="Name" class="items" />


Now in your GET controller, create an object of the ShoppingList and send to the view.



public IActionResult ShoppingList()
{
var vm = new ShoppingList() { };
return View(vm);
}


Now in the main view, All you have to do is call the EditorFor method



@model YourNamespace.ShoppingList
<form asp-action="ShoppingList" method="post">
<input asp-for="Name" class="form-control" />
<div class="form-group" id="item-list">
<a href="#" id="add">Add</a>
@Html.EditorFor(f => f.ListItems)
</div>
<input type="submit" value="Create" class="btn btn-default" />
</form>


The markup has an anchor tag for adding new items. So when user clicks on it, we need to add a new input element with the name attribute value in the format ListItems[indexValue].Name



$(function () {

$("#add").click(function (e) {
e.preventDefault();
var i = $(".items").length;
var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
$("#item-list").append(n);
});

});


So when user clicks it adds a new input element with the correct name to the DOM and when you click the submit button model binding will work fine as we have the correct name attribute value for the inputs.



[HttpPost]
public IActionResult ShoppingList(ShoppingList model)
{
//check model.ListItems
// to do : return something
}


If you want to preload some existing items (for edit screen etc), All you have to do is load the ListItems property and the editor template will take care of rendering the input elements for each item with correct name attribute value.



public IActionResult ShoppingList()
{
var vm = new ShoppingList();
vm.ListItems = new List<Item>() { new Item { Name = "apple" } }
return View(vm);
}





share|improve this answer





























    0














    First this is you must have a public accessor to your ShoppingList class.



    So, public class ShoppingList.



    Next is your view will need the following changes.



    @model ShoppingList

    <h1>@Model.Name</h1>
    <h2>@Model.ShoppingListId</h2>
    foreach(var item in Model.ListItems)
    {
    <h3>@item</h3>
    }


    So, the above code is roughly what you are looking for.
    In Razor you can accessor the models variables by using the @model at the top of the view. But one thing you need to note is if your model is in a subfolder you'll need to dot into that.



    Here's an example: @model BethanysPieShop.Models.ShoppingCart.



    Here BethanysPieShop is my project name, Models is my folder the ShoppingCart class is in.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46854085%2fcreate-dynamic-forms-that-grow-at-run-time%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      If you want to allow the user to add a new form element on the client side you need to use javascript to update the DOM with the new element you want to add. To list the existing items you may use editor templates. Mixing these 2 will give you a dynamic form. The below is a basic implementation.



      To use editor templates, we need to create an editor template for the property type. I would not do that for string type which is more like a generic one. I would create a custom class to represent the list item.



      public class Item
      {
      public string Name { set; get; }
      }
      public class ShoppingList
      {
      public int ShoppingListId { get; set; }
      public string Name { get; set; }
      public List<Item> ListItems { get; set; }

      public ShoppingList()
      {
      this.ListItems=new List<Item>();
      }
      }


      Now, Create a directory called EditorTemplates under ~/Views/YourControllerName or ~/Views/Shared/ and create a view called Item.cshtml which will have the below code



      @model  YourNameSpaceHere.Item
      <input type="text" asp-for="Name" class="items" />


      Now in your GET controller, create an object of the ShoppingList and send to the view.



      public IActionResult ShoppingList()
      {
      var vm = new ShoppingList() { };
      return View(vm);
      }


      Now in the main view, All you have to do is call the EditorFor method



      @model YourNamespace.ShoppingList
      <form asp-action="ShoppingList" method="post">
      <input asp-for="Name" class="form-control" />
      <div class="form-group" id="item-list">
      <a href="#" id="add">Add</a>
      @Html.EditorFor(f => f.ListItems)
      </div>
      <input type="submit" value="Create" class="btn btn-default" />
      </form>


      The markup has an anchor tag for adding new items. So when user clicks on it, we need to add a new input element with the name attribute value in the format ListItems[indexValue].Name



      $(function () {

      $("#add").click(function (e) {
      e.preventDefault();
      var i = $(".items").length;
      var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
      $("#item-list").append(n);
      });

      });


      So when user clicks it adds a new input element with the correct name to the DOM and when you click the submit button model binding will work fine as we have the correct name attribute value for the inputs.



      [HttpPost]
      public IActionResult ShoppingList(ShoppingList model)
      {
      //check model.ListItems
      // to do : return something
      }


      If you want to preload some existing items (for edit screen etc), All you have to do is load the ListItems property and the editor template will take care of rendering the input elements for each item with correct name attribute value.



      public IActionResult ShoppingList()
      {
      var vm = new ShoppingList();
      vm.ListItems = new List<Item>() { new Item { Name = "apple" } }
      return View(vm);
      }





      share|improve this answer


























        5














        If you want to allow the user to add a new form element on the client side you need to use javascript to update the DOM with the new element you want to add. To list the existing items you may use editor templates. Mixing these 2 will give you a dynamic form. The below is a basic implementation.



        To use editor templates, we need to create an editor template for the property type. I would not do that for string type which is more like a generic one. I would create a custom class to represent the list item.



        public class Item
        {
        public string Name { set; get; }
        }
        public class ShoppingList
        {
        public int ShoppingListId { get; set; }
        public string Name { get; set; }
        public List<Item> ListItems { get; set; }

        public ShoppingList()
        {
        this.ListItems=new List<Item>();
        }
        }


        Now, Create a directory called EditorTemplates under ~/Views/YourControllerName or ~/Views/Shared/ and create a view called Item.cshtml which will have the below code



        @model  YourNameSpaceHere.Item
        <input type="text" asp-for="Name" class="items" />


        Now in your GET controller, create an object of the ShoppingList and send to the view.



        public IActionResult ShoppingList()
        {
        var vm = new ShoppingList() { };
        return View(vm);
        }


        Now in the main view, All you have to do is call the EditorFor method



        @model YourNamespace.ShoppingList
        <form asp-action="ShoppingList" method="post">
        <input asp-for="Name" class="form-control" />
        <div class="form-group" id="item-list">
        <a href="#" id="add">Add</a>
        @Html.EditorFor(f => f.ListItems)
        </div>
        <input type="submit" value="Create" class="btn btn-default" />
        </form>


        The markup has an anchor tag for adding new items. So when user clicks on it, we need to add a new input element with the name attribute value in the format ListItems[indexValue].Name



        $(function () {

        $("#add").click(function (e) {
        e.preventDefault();
        var i = $(".items").length;
        var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
        $("#item-list").append(n);
        });

        });


        So when user clicks it adds a new input element with the correct name to the DOM and when you click the submit button model binding will work fine as we have the correct name attribute value for the inputs.



        [HttpPost]
        public IActionResult ShoppingList(ShoppingList model)
        {
        //check model.ListItems
        // to do : return something
        }


        If you want to preload some existing items (for edit screen etc), All you have to do is load the ListItems property and the editor template will take care of rendering the input elements for each item with correct name attribute value.



        public IActionResult ShoppingList()
        {
        var vm = new ShoppingList();
        vm.ListItems = new List<Item>() { new Item { Name = "apple" } }
        return View(vm);
        }





        share|improve this answer
























          5












          5








          5






          If you want to allow the user to add a new form element on the client side you need to use javascript to update the DOM with the new element you want to add. To list the existing items you may use editor templates. Mixing these 2 will give you a dynamic form. The below is a basic implementation.



          To use editor templates, we need to create an editor template for the property type. I would not do that for string type which is more like a generic one. I would create a custom class to represent the list item.



          public class Item
          {
          public string Name { set; get; }
          }
          public class ShoppingList
          {
          public int ShoppingListId { get; set; }
          public string Name { get; set; }
          public List<Item> ListItems { get; set; }

          public ShoppingList()
          {
          this.ListItems=new List<Item>();
          }
          }


          Now, Create a directory called EditorTemplates under ~/Views/YourControllerName or ~/Views/Shared/ and create a view called Item.cshtml which will have the below code



          @model  YourNameSpaceHere.Item
          <input type="text" asp-for="Name" class="items" />


          Now in your GET controller, create an object of the ShoppingList and send to the view.



          public IActionResult ShoppingList()
          {
          var vm = new ShoppingList() { };
          return View(vm);
          }


          Now in the main view, All you have to do is call the EditorFor method



          @model YourNamespace.ShoppingList
          <form asp-action="ShoppingList" method="post">
          <input asp-for="Name" class="form-control" />
          <div class="form-group" id="item-list">
          <a href="#" id="add">Add</a>
          @Html.EditorFor(f => f.ListItems)
          </div>
          <input type="submit" value="Create" class="btn btn-default" />
          </form>


          The markup has an anchor tag for adding new items. So when user clicks on it, we need to add a new input element with the name attribute value in the format ListItems[indexValue].Name



          $(function () {

          $("#add").click(function (e) {
          e.preventDefault();
          var i = $(".items").length;
          var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
          $("#item-list").append(n);
          });

          });


          So when user clicks it adds a new input element with the correct name to the DOM and when you click the submit button model binding will work fine as we have the correct name attribute value for the inputs.



          [HttpPost]
          public IActionResult ShoppingList(ShoppingList model)
          {
          //check model.ListItems
          // to do : return something
          }


          If you want to preload some existing items (for edit screen etc), All you have to do is load the ListItems property and the editor template will take care of rendering the input elements for each item with correct name attribute value.



          public IActionResult ShoppingList()
          {
          var vm = new ShoppingList();
          vm.ListItems = new List<Item>() { new Item { Name = "apple" } }
          return View(vm);
          }





          share|improve this answer












          If you want to allow the user to add a new form element on the client side you need to use javascript to update the DOM with the new element you want to add. To list the existing items you may use editor templates. Mixing these 2 will give you a dynamic form. The below is a basic implementation.



          To use editor templates, we need to create an editor template for the property type. I would not do that for string type which is more like a generic one. I would create a custom class to represent the list item.



          public class Item
          {
          public string Name { set; get; }
          }
          public class ShoppingList
          {
          public int ShoppingListId { get; set; }
          public string Name { get; set; }
          public List<Item> ListItems { get; set; }

          public ShoppingList()
          {
          this.ListItems=new List<Item>();
          }
          }


          Now, Create a directory called EditorTemplates under ~/Views/YourControllerName or ~/Views/Shared/ and create a view called Item.cshtml which will have the below code



          @model  YourNameSpaceHere.Item
          <input type="text" asp-for="Name" class="items" />


          Now in your GET controller, create an object of the ShoppingList and send to the view.



          public IActionResult ShoppingList()
          {
          var vm = new ShoppingList() { };
          return View(vm);
          }


          Now in the main view, All you have to do is call the EditorFor method



          @model YourNamespace.ShoppingList
          <form asp-action="ShoppingList" method="post">
          <input asp-for="Name" class="form-control" />
          <div class="form-group" id="item-list">
          <a href="#" id="add">Add</a>
          @Html.EditorFor(f => f.ListItems)
          </div>
          <input type="submit" value="Create" class="btn btn-default" />
          </form>


          The markup has an anchor tag for adding new items. So when user clicks on it, we need to add a new input element with the name attribute value in the format ListItems[indexValue].Name



          $(function () {

          $("#add").click(function (e) {
          e.preventDefault();
          var i = $(".items").length;
          var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
          $("#item-list").append(n);
          });

          });


          So when user clicks it adds a new input element with the correct name to the DOM and when you click the submit button model binding will work fine as we have the correct name attribute value for the inputs.



          [HttpPost]
          public IActionResult ShoppingList(ShoppingList model)
          {
          //check model.ListItems
          // to do : return something
          }


          If you want to preload some existing items (for edit screen etc), All you have to do is load the ListItems property and the editor template will take care of rendering the input elements for each item with correct name attribute value.



          public IActionResult ShoppingList()
          {
          var vm = new ShoppingList();
          vm.ListItems = new List<Item>() { new Item { Name = "apple" } }
          return View(vm);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 20 '17 at 17:52









          Shyju

          143k87329435




          143k87329435

























              0














              First this is you must have a public accessor to your ShoppingList class.



              So, public class ShoppingList.



              Next is your view will need the following changes.



              @model ShoppingList

              <h1>@Model.Name</h1>
              <h2>@Model.ShoppingListId</h2>
              foreach(var item in Model.ListItems)
              {
              <h3>@item</h3>
              }


              So, the above code is roughly what you are looking for.
              In Razor you can accessor the models variables by using the @model at the top of the view. But one thing you need to note is if your model is in a subfolder you'll need to dot into that.



              Here's an example: @model BethanysPieShop.Models.ShoppingCart.



              Here BethanysPieShop is my project name, Models is my folder the ShoppingCart class is in.






              share|improve this answer


























                0














                First this is you must have a public accessor to your ShoppingList class.



                So, public class ShoppingList.



                Next is your view will need the following changes.



                @model ShoppingList

                <h1>@Model.Name</h1>
                <h2>@Model.ShoppingListId</h2>
                foreach(var item in Model.ListItems)
                {
                <h3>@item</h3>
                }


                So, the above code is roughly what you are looking for.
                In Razor you can accessor the models variables by using the @model at the top of the view. But one thing you need to note is if your model is in a subfolder you'll need to dot into that.



                Here's an example: @model BethanysPieShop.Models.ShoppingCart.



                Here BethanysPieShop is my project name, Models is my folder the ShoppingCart class is in.






                share|improve this answer
























                  0












                  0








                  0






                  First this is you must have a public accessor to your ShoppingList class.



                  So, public class ShoppingList.



                  Next is your view will need the following changes.



                  @model ShoppingList

                  <h1>@Model.Name</h1>
                  <h2>@Model.ShoppingListId</h2>
                  foreach(var item in Model.ListItems)
                  {
                  <h3>@item</h3>
                  }


                  So, the above code is roughly what you are looking for.
                  In Razor you can accessor the models variables by using the @model at the top of the view. But one thing you need to note is if your model is in a subfolder you'll need to dot into that.



                  Here's an example: @model BethanysPieShop.Models.ShoppingCart.



                  Here BethanysPieShop is my project name, Models is my folder the ShoppingCart class is in.






                  share|improve this answer












                  First this is you must have a public accessor to your ShoppingList class.



                  So, public class ShoppingList.



                  Next is your view will need the following changes.



                  @model ShoppingList

                  <h1>@Model.Name</h1>
                  <h2>@Model.ShoppingListId</h2>
                  foreach(var item in Model.ListItems)
                  {
                  <h3>@item</h3>
                  }


                  So, the above code is roughly what you are looking for.
                  In Razor you can accessor the models variables by using the @model at the top of the view. But one thing you need to note is if your model is in a subfolder you'll need to dot into that.



                  Here's an example: @model BethanysPieShop.Models.ShoppingCart.



                  Here BethanysPieShop is my project name, Models is my folder the ShoppingCart class is in.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 20 '17 at 17:30









                  Jared

                  283112




                  283112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46854085%2fcreate-dynamic-forms-that-grow-at-run-time%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python