Html.TextBoxFor input inside for each loop not working
up vote
0
down vote
favorite
I have some fields which may vary according to type of project. I'm trying to create these fields and take values for them. I tried create labels and TextBoxFor input by iterating inside a foreach loop like this;
<div class="row" id="customerdetailcontent">
@foreach (var detail in customerFields)
{
<div class="form-group">
<label class="col-md-3 control-label">@detail</label>
<div class="col-md-9">
@Html.TextBoxFor(model => model.CustomerDetail, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CustomerDetail, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
When I click Submit and check the parameter arriving to action inside controller, CustomerDetail list is null.
this is the field declaration inside the model
public List<CustomerDetailModel> CustomerDetail { get; set; }
It's been a while since I wrote Razor, I might be making a simple mistake or trying something impossible. What am I doing wrong?
razor html.textboxfor
add a comment |
up vote
0
down vote
favorite
I have some fields which may vary according to type of project. I'm trying to create these fields and take values for them. I tried create labels and TextBoxFor input by iterating inside a foreach loop like this;
<div class="row" id="customerdetailcontent">
@foreach (var detail in customerFields)
{
<div class="form-group">
<label class="col-md-3 control-label">@detail</label>
<div class="col-md-9">
@Html.TextBoxFor(model => model.CustomerDetail, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CustomerDetail, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
When I click Submit and check the parameter arriving to action inside controller, CustomerDetail list is null.
this is the field declaration inside the model
public List<CustomerDetailModel> CustomerDetail { get; set; }
It's been a while since I wrote Razor, I might be making a simple mistake or trying something impossible. What am I doing wrong?
razor html.textboxfor
You cannot bind a textbox to a complex property - it binds to a posts back a simple value (e.g. astring
). And you cannot use aforeach
loop to generate form controls for a collection. Refer Post an HTML Table to ADO.NET DataTable. You have not shown your model, but you need something like@for(int i = 0; i < Mdoel.CustomerDetail.Count; i++) { @Html.TextBoxFor(m => m.CustomerDetail[i].SomeProperty) }
– user3559349
Nov 19 at 22:43
@StephenMuecke thank you, your comment solved the problem. If you re-send it as an anwer I'd be happy to mark it as accepted answer.
– Ege Bayrak
Nov 20 at 12:23
Your question is lacking information and is a a bit vague and unclear so adding an answer as it stands would not be of help to others
– user3559349
Nov 22 at 2:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have some fields which may vary according to type of project. I'm trying to create these fields and take values for them. I tried create labels and TextBoxFor input by iterating inside a foreach loop like this;
<div class="row" id="customerdetailcontent">
@foreach (var detail in customerFields)
{
<div class="form-group">
<label class="col-md-3 control-label">@detail</label>
<div class="col-md-9">
@Html.TextBoxFor(model => model.CustomerDetail, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CustomerDetail, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
When I click Submit and check the parameter arriving to action inside controller, CustomerDetail list is null.
this is the field declaration inside the model
public List<CustomerDetailModel> CustomerDetail { get; set; }
It's been a while since I wrote Razor, I might be making a simple mistake or trying something impossible. What am I doing wrong?
razor html.textboxfor
I have some fields which may vary according to type of project. I'm trying to create these fields and take values for them. I tried create labels and TextBoxFor input by iterating inside a foreach loop like this;
<div class="row" id="customerdetailcontent">
@foreach (var detail in customerFields)
{
<div class="form-group">
<label class="col-md-3 control-label">@detail</label>
<div class="col-md-9">
@Html.TextBoxFor(model => model.CustomerDetail, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CustomerDetail, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
When I click Submit and check the parameter arriving to action inside controller, CustomerDetail list is null.
this is the field declaration inside the model
public List<CustomerDetailModel> CustomerDetail { get; set; }
It's been a while since I wrote Razor, I might be making a simple mistake or trying something impossible. What am I doing wrong?
razor html.textboxfor
razor html.textboxfor
asked Nov 19 at 14:57
Ege Bayrak
398624
398624
You cannot bind a textbox to a complex property - it binds to a posts back a simple value (e.g. astring
). And you cannot use aforeach
loop to generate form controls for a collection. Refer Post an HTML Table to ADO.NET DataTable. You have not shown your model, but you need something like@for(int i = 0; i < Mdoel.CustomerDetail.Count; i++) { @Html.TextBoxFor(m => m.CustomerDetail[i].SomeProperty) }
– user3559349
Nov 19 at 22:43
@StephenMuecke thank you, your comment solved the problem. If you re-send it as an anwer I'd be happy to mark it as accepted answer.
– Ege Bayrak
Nov 20 at 12:23
Your question is lacking information and is a a bit vague and unclear so adding an answer as it stands would not be of help to others
– user3559349
Nov 22 at 2:19
add a comment |
You cannot bind a textbox to a complex property - it binds to a posts back a simple value (e.g. astring
). And you cannot use aforeach
loop to generate form controls for a collection. Refer Post an HTML Table to ADO.NET DataTable. You have not shown your model, but you need something like@for(int i = 0; i < Mdoel.CustomerDetail.Count; i++) { @Html.TextBoxFor(m => m.CustomerDetail[i].SomeProperty) }
– user3559349
Nov 19 at 22:43
@StephenMuecke thank you, your comment solved the problem. If you re-send it as an anwer I'd be happy to mark it as accepted answer.
– Ege Bayrak
Nov 20 at 12:23
Your question is lacking information and is a a bit vague and unclear so adding an answer as it stands would not be of help to others
– user3559349
Nov 22 at 2:19
You cannot bind a textbox to a complex property - it binds to a posts back a simple value (e.g. a
string
). And you cannot use a foreach
loop to generate form controls for a collection. Refer Post an HTML Table to ADO.NET DataTable. You have not shown your model, but you need something like @for(int i = 0; i < Mdoel.CustomerDetail.Count; i++) { @Html.TextBoxFor(m => m.CustomerDetail[i].SomeProperty) }
– user3559349
Nov 19 at 22:43
You cannot bind a textbox to a complex property - it binds to a posts back a simple value (e.g. a
string
). And you cannot use a foreach
loop to generate form controls for a collection. Refer Post an HTML Table to ADO.NET DataTable. You have not shown your model, but you need something like @for(int i = 0; i < Mdoel.CustomerDetail.Count; i++) { @Html.TextBoxFor(m => m.CustomerDetail[i].SomeProperty) }
– user3559349
Nov 19 at 22:43
@StephenMuecke thank you, your comment solved the problem. If you re-send it as an anwer I'd be happy to mark it as accepted answer.
– Ege Bayrak
Nov 20 at 12:23
@StephenMuecke thank you, your comment solved the problem. If you re-send it as an anwer I'd be happy to mark it as accepted answer.
– Ege Bayrak
Nov 20 at 12:23
Your question is lacking information and is a a bit vague and unclear so adding an answer as it stands would not be of help to others
– user3559349
Nov 22 at 2:19
Your question is lacking information and is a a bit vague and unclear so adding an answer as it stands would not be of help to others
– user3559349
Nov 22 at 2:19
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53377272%2fhtml-textboxfor-input-inside-for-each-loop-not-working%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
You cannot bind a textbox to a complex property - it binds to a posts back a simple value (e.g. a
string
). And you cannot use aforeach
loop to generate form controls for a collection. Refer Post an HTML Table to ADO.NET DataTable. You have not shown your model, but you need something like@for(int i = 0; i < Mdoel.CustomerDetail.Count; i++) { @Html.TextBoxFor(m => m.CustomerDetail[i].SomeProperty) }
– user3559349
Nov 19 at 22:43
@StephenMuecke thank you, your comment solved the problem. If you re-send it as an anwer I'd be happy to mark it as accepted answer.
– Ege Bayrak
Nov 20 at 12:23
Your question is lacking information and is a a bit vague and unclear so adding an answer as it stands would not be of help to others
– user3559349
Nov 22 at 2:19