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( /* ??? /*)
c# razor asp.net-core
|
show 1 more comment
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( /* ??? /*)
c# razor asp.net-core
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
|
show 1 more comment
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( /* ??? /*)
c# razor asp.net-core
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
c# razor asp.net-core
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
|
show 1 more comment
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
|
show 1 more comment
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)
add a comment |
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?
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 13 at 1:50
answered Nov 12 at 9:55
Xing Zou
3114
3114
add a comment |
add a comment |
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?
add a comment |
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?
add a comment |
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?
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?
answered Nov 19 at 20:17
Iliyan Ivanov
578
578
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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