Bind dynamic inputs











up vote
0
down vote

favorite












I have a form that has a dynamic inputs, so I don't know the count and names of the inputs, as you can see in the code below, they came from database.



 <div class="panel-body">
<form asp-action="BrandFilter" method="get">
<div class="form-group">
@foreach (var brand in Model)
{
<div class="checkbox">
<label>
<input asp-for="@brand" type="checkbox" />@brand (10)
</label>
</div>
}
</div>
<button class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Apply</button>
</form>
</div>


The question is how to bind all inputs when form is submitted.



public async Task<IActionResult> BrandFilter( /* ??? /*)









share|improve this question






















  • One option is using editor templates.
    – Shyju
    Nov 12 at 4:13










  • I don't think that can help me in this case.
    – Iliyan Ivanov
    Nov 12 at 5:00










  • why do you think so ?
    – Shyju
    Nov 12 at 5:17










  • Because I can't understand how editor template is useful in this case, if you think so answer the question and show me how with example. :)
    – Iliyan Ivanov
    Nov 12 at 6:28










  • What is @brand in your case? Could you show us the Model and the @Model of your view? I guess that you would like to pass List<Model> to controller?
    – Xing Zou
    Nov 12 at 10:01















up vote
0
down vote

favorite












I have a form that has a dynamic inputs, so I don't know the count and names of the inputs, as you can see in the code below, they came from database.



 <div class="panel-body">
<form asp-action="BrandFilter" method="get">
<div class="form-group">
@foreach (var brand in Model)
{
<div class="checkbox">
<label>
<input asp-for="@brand" type="checkbox" />@brand (10)
</label>
</div>
}
</div>
<button class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Apply</button>
</form>
</div>


The question is how to bind all inputs when form is submitted.



public async Task<IActionResult> BrandFilter( /* ??? /*)









share|improve this question






















  • One option is using editor templates.
    – Shyju
    Nov 12 at 4:13










  • I don't think that can help me in this case.
    – Iliyan Ivanov
    Nov 12 at 5:00










  • why do you think so ?
    – Shyju
    Nov 12 at 5:17










  • Because I can't understand how editor template is useful in this case, if you think so answer the question and show me how with example. :)
    – Iliyan Ivanov
    Nov 12 at 6:28










  • What is @brand in your case? Could you show us the Model and the @Model of your view? I guess that you would like to pass List<Model> to controller?
    – Xing Zou
    Nov 12 at 10:01













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a form that has a dynamic inputs, so I don't know the count and names of the inputs, as you can see in the code below, they came from database.



 <div class="panel-body">
<form asp-action="BrandFilter" method="get">
<div class="form-group">
@foreach (var brand in Model)
{
<div class="checkbox">
<label>
<input asp-for="@brand" type="checkbox" />@brand (10)
</label>
</div>
}
</div>
<button class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Apply</button>
</form>
</div>


The question is how to bind all inputs when form is submitted.



public async Task<IActionResult> BrandFilter( /* ??? /*)









share|improve this question













I have a form that has a dynamic inputs, so I don't know the count and names of the inputs, as you can see in the code below, they came from database.



 <div class="panel-body">
<form asp-action="BrandFilter" method="get">
<div class="form-group">
@foreach (var brand in Model)
{
<div class="checkbox">
<label>
<input asp-for="@brand" type="checkbox" />@brand (10)
</label>
</div>
}
</div>
<button class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Apply</button>
</form>
</div>


The question is how to bind all inputs when form is submitted.



public async Task<IActionResult> BrandFilter( /* ??? /*)






c# razor asp.net-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 4:09









Iliyan Ivanov

578




578












  • One option is using editor templates.
    – Shyju
    Nov 12 at 4:13










  • I don't think that can help me in this case.
    – Iliyan Ivanov
    Nov 12 at 5:00










  • why do you think so ?
    – Shyju
    Nov 12 at 5:17










  • Because I can't understand how editor template is useful in this case, if you think so answer the question and show me how with example. :)
    – Iliyan Ivanov
    Nov 12 at 6:28










  • What is @brand in your case? Could you show us the Model and the @Model of your view? I guess that you would like to pass List<Model> to controller?
    – Xing Zou
    Nov 12 at 10:01


















  • One option is using editor templates.
    – Shyju
    Nov 12 at 4:13










  • I don't think that can help me in this case.
    – Iliyan Ivanov
    Nov 12 at 5:00










  • why do you think so ?
    – Shyju
    Nov 12 at 5:17










  • Because I can't understand how editor template is useful in this case, if you think so answer the question and show me how with example. :)
    – Iliyan Ivanov
    Nov 12 at 6:28










  • What is @brand in your case? Could you show us the Model and the @Model of your view? I guess that you would like to pass List<Model> to controller?
    – Xing Zou
    Nov 12 at 10:01
















One option is using editor templates.
– Shyju
Nov 12 at 4:13




One option is using editor templates.
– Shyju
Nov 12 at 4:13












I don't think that can help me in this case.
– Iliyan Ivanov
Nov 12 at 5:00




I don't think that can help me in this case.
– Iliyan Ivanov
Nov 12 at 5:00












why do you think so ?
– Shyju
Nov 12 at 5:17




why do you think so ?
– Shyju
Nov 12 at 5:17












Because I can't understand how editor template is useful in this case, if you think so answer the question and show me how with example. :)
– Iliyan Ivanov
Nov 12 at 6:28




Because I can't understand how editor template is useful in this case, if you think so answer the question and show me how with example. :)
– Iliyan Ivanov
Nov 12 at 6:28












What is @brand in your case? Could you show us the Model and the @Model of your view? I guess that you would like to pass List<Model> to controller?
– Xing Zou
Nov 12 at 10:01




What is @brand in your case? Could you show us the Model and the @Model of your view? I guess that you would like to pass List<Model> to controller?
– Xing Zou
Nov 12 at 10:01












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










I create a demo that you could select the brands and pass all checkboxes' values to controller.



1.Brand.cs



public class Brand
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public bool Selected { get; set; }
}


2.View



@model List<Models.Brand>
<div class="col-md-4">
<form asp-action="BrandFilter" method="post">
<div class="form-group">

@for (int i = 0; i < Model.Count(); i++)
{
<div class="checkbox">
@Html.HiddenFor(m => m[i].Name)
@Html.CheckBoxFor(m => m[i].Selected)
@Html.DisplayFor(m => m[i].Name)
</div>
}

</div>

<div class="form-group">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</form>
</div>




3.controller



 public async Task<IActionResult> BrandFilter(List<Brand> brands)





share|improve this answer






























    up vote
    0
    down vote













    The above answer actually works, but I have found also an another approach.



    In CSHTML file:



    <input name="@brand.Name" type="checkbox" value="true"/>@brand.Name (@brand.Count)


    Method signature:



     public async Task<IActionResult> BrandFilter(Dictionary<string, bool> brands)


    In this approach you are receiving only the checked inputs which leads to a way less data sent to the server.



    So instead of posting another question, comment under this answer and say What do you think for both approaches and which one is better?






    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',
      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%2f53255850%2fbind-dynamic-inputs%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








      up vote
      1
      down vote



      accepted










      I create a demo that you could select the brands and pass all checkboxes' values to controller.



      1.Brand.cs



      public class Brand
      {
      [Key]
      public int ID { get; set; }
      public string Name { get; set; }
      public bool Selected { get; set; }
      }


      2.View



      @model List<Models.Brand>
      <div class="col-md-4">
      <form asp-action="BrandFilter" method="post">
      <div class="form-group">

      @for (int i = 0; i < Model.Count(); i++)
      {
      <div class="checkbox">
      @Html.HiddenFor(m => m[i].Name)
      @Html.CheckBoxFor(m => m[i].Selected)
      @Html.DisplayFor(m => m[i].Name)
      </div>
      }

      </div>

      <div class="form-group">
      <input type="submit" value="Submit" class="btn btn-default" />
      </div>
      </form>
      </div>




      3.controller



       public async Task<IActionResult> BrandFilter(List<Brand> brands)





      share|improve this answer



























        up vote
        1
        down vote



        accepted










        I create a demo that you could select the brands and pass all checkboxes' values to controller.



        1.Brand.cs



        public class Brand
        {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
        public bool Selected { get; set; }
        }


        2.View



        @model List<Models.Brand>
        <div class="col-md-4">
        <form asp-action="BrandFilter" method="post">
        <div class="form-group">

        @for (int i = 0; i < Model.Count(); i++)
        {
        <div class="checkbox">
        @Html.HiddenFor(m => m[i].Name)
        @Html.CheckBoxFor(m => m[i].Selected)
        @Html.DisplayFor(m => m[i].Name)
        </div>
        }

        </div>

        <div class="form-group">
        <input type="submit" value="Submit" class="btn btn-default" />
        </div>
        </form>
        </div>




        3.controller



         public async Task<IActionResult> BrandFilter(List<Brand> brands)





        share|improve this answer

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I create a demo that you could select the brands and pass all checkboxes' values to controller.



          1.Brand.cs



          public class Brand
          {
          [Key]
          public int ID { get; set; }
          public string Name { get; set; }
          public bool Selected { get; set; }
          }


          2.View



          @model List<Models.Brand>
          <div class="col-md-4">
          <form asp-action="BrandFilter" method="post">
          <div class="form-group">

          @for (int i = 0; i < Model.Count(); i++)
          {
          <div class="checkbox">
          @Html.HiddenFor(m => m[i].Name)
          @Html.CheckBoxFor(m => m[i].Selected)
          @Html.DisplayFor(m => m[i].Name)
          </div>
          }

          </div>

          <div class="form-group">
          <input type="submit" value="Submit" class="btn btn-default" />
          </div>
          </form>
          </div>




          3.controller



           public async Task<IActionResult> BrandFilter(List<Brand> brands)





          share|improve this answer














          I create a demo that you could select the brands and pass all checkboxes' values to controller.



          1.Brand.cs



          public class Brand
          {
          [Key]
          public int ID { get; set; }
          public string Name { get; set; }
          public bool Selected { get; set; }
          }


          2.View



          @model List<Models.Brand>
          <div class="col-md-4">
          <form asp-action="BrandFilter" method="post">
          <div class="form-group">

          @for (int i = 0; i < Model.Count(); i++)
          {
          <div class="checkbox">
          @Html.HiddenFor(m => m[i].Name)
          @Html.CheckBoxFor(m => m[i].Selected)
          @Html.DisplayFor(m => m[i].Name)
          </div>
          }

          </div>

          <div class="form-group">
          <input type="submit" value="Submit" class="btn btn-default" />
          </div>
          </form>
          </div>




          3.controller



           public async Task<IActionResult> BrandFilter(List<Brand> brands)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 1:50

























          answered Nov 12 at 9:55









          Xing Zou

          3114




          3114
























              up vote
              0
              down vote













              The above answer actually works, but I have found also an another approach.



              In CSHTML file:



              <input name="@brand.Name" type="checkbox" value="true"/>@brand.Name (@brand.Count)


              Method signature:



               public async Task<IActionResult> BrandFilter(Dictionary<string, bool> brands)


              In this approach you are receiving only the checked inputs which leads to a way less data sent to the server.



              So instead of posting another question, comment under this answer and say What do you think for both approaches and which one is better?






              share|improve this answer

























                up vote
                0
                down vote













                The above answer actually works, but I have found also an another approach.



                In CSHTML file:



                <input name="@brand.Name" type="checkbox" value="true"/>@brand.Name (@brand.Count)


                Method signature:



                 public async Task<IActionResult> BrandFilter(Dictionary<string, bool> brands)


                In this approach you are receiving only the checked inputs which leads to a way less data sent to the server.



                So instead of posting another question, comment under this answer and say What do you think for both approaches and which one is better?






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The above answer actually works, but I have found also an another approach.



                  In CSHTML file:



                  <input name="@brand.Name" type="checkbox" value="true"/>@brand.Name (@brand.Count)


                  Method signature:



                   public async Task<IActionResult> BrandFilter(Dictionary<string, bool> brands)


                  In this approach you are receiving only the checked inputs which leads to a way less data sent to the server.



                  So instead of posting another question, comment under this answer and say What do you think for both approaches and which one is better?






                  share|improve this answer












                  The above answer actually works, but I have found also an another approach.



                  In CSHTML file:



                  <input name="@brand.Name" type="checkbox" value="true"/>@brand.Name (@brand.Count)


                  Method signature:



                   public async Task<IActionResult> BrandFilter(Dictionary<string, bool> brands)


                  In this approach you are receiving only the checked inputs which leads to a way less data sent to the server.



                  So instead of posting another question, comment under this answer and say What do you think for both approaches and which one is better?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 20:17









                  Iliyan Ivanov

                  578




                  578






























                      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%2f53255850%2fbind-dynamic-inputs%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

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)