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?










share|improve this question






















  • 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










  • 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















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?










share|improve this question






















  • 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










  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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. 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










  • 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










  • @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

















active

oldest

votes











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%2f53377272%2fhtml-textboxfor-input-inside-for-each-loop-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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

Wiesbaden

Marschland

Dieringhausen