How to bind a list of objects in ASP.NET Core MVC when the posted list is smaller than original?ASP.NET MVC Model list bindingASP.NET MVC 3 model binding of complex objectHow does MVC 4 List Model Binding work?ASP.Net MVC form post can't bind model list propertyCheckBoxFor failing to bind to complex object (MVC)asp.net mvc 6 model binding to complex collection - IList<T>MVC Dynamic List bindingASP.NET Core Model Bind Collection of Unknown LengthASP.NET Core MVC - empty string to null when sending JSON to serverASP.NET Core MVC Model Binding Bug?
Were Roman public roads build by private companies?
Should you only use colons and periods in dialogues?
Can I conceal an antihero's insanity - and should I?
Which is the current decimal separator?
Does my opponent need to prove his creature has morph?
Does an oscilloscope subtract voltages as phasors?
Why is my fire extinguisher emptied after one use?
What officially disallows US presidents from driving?
How to stabilise the bicycle seatpost and saddle when it is all the way up?
Are Democrats more likely to believe Astrology is a science?
Can I fix my boots by gluing the soles back on?
What's the orientation of the Rope Trick hole?
How to remove a secondary database from an availability group and rejoin it?
How do email clients "send later" without storing a password?
In Germany, how can I maximize the impact of my charitable donations?
What was redacted in the Yellowhammer report? (Point 15)
Confirm the ending of a string
Parallel resistance in electric circuits
Percentage buffer around multiline in QGIS?
Has SHA256 been broken by Treadwell Stanton DuPont?
Writing a love interest for my hero
Planar regular languages
Why is the Digital 0 not 0V in computer systems?
"Literally" Vs "In the true sense of the word"
How to bind a list of objects in ASP.NET Core MVC when the posted list is smaller than original?
ASP.NET MVC Model list bindingASP.NET MVC 3 model binding of complex objectHow does MVC 4 List Model Binding work?ASP.Net MVC form post can't bind model list propertyCheckBoxFor failing to bind to complex object (MVC)asp.net mvc 6 model binding to complex collection - IList<T>MVC Dynamic List bindingASP.NET Core Model Bind Collection of Unknown LengthASP.NET Core MVC - empty string to null when sending JSON to serverASP.NET Core MVC Model Binding Bug?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Using "disabled" attribute on inputs on form does not post them, which is expected and wanted. However, if you prepare a form of 3 objects in a list, disable the first and third, and submit, the 2nd object appears in post header, but does not bind to the list correctly, because it has an index [1] instead of [0].
I understand how model binding works and why it does not bind the posted object that I want, but I don't know how else to describe the problem to get specific results that would lead me to my solution. Anything I search for leads to basic post and binding examples.
List inside the model I'm using:
public IList<_Result> Results get; set;
Class _Result has one of the properties:
public string Value get; set;
I fill up the list and use it in view like so:
@for (int i = 0; i < Model.Results.Count; i++)
...
<td>
<input asp-for="Results[i].Value" disabled />
</td>
...
I have checkboxes on form, which remove (with javascript) the "disabled" attribute from the inputs and thus allow them to be posted.
When I fill up the said list with 3 _Result objects, they are shown on form and all have the "disabled" attribute. If I remove the "disabled" attribute from the first two objects and click on submit button, I receive the Results list with first 2 _Result objects, which is as expected.
However, if I remove the "disabled" attribute only from the second _Result object (the first _Result object still has "disabled" attribute), the Results list comes back empty in my Controller method.
In my Form Data Header, I see this: "Results[1].Value: Value that I want posted", which means that post occurs, but list does not bind the object due to the index.
Any idea on how I can achieve that proper binding? Also, the reason I'm using "disabled" attribute is because I'm showing many results on a single page and want to only post those that are selected.
view asp.net-core-mvc model-binding
add a comment
|
Using "disabled" attribute on inputs on form does not post them, which is expected and wanted. However, if you prepare a form of 3 objects in a list, disable the first and third, and submit, the 2nd object appears in post header, but does not bind to the list correctly, because it has an index [1] instead of [0].
I understand how model binding works and why it does not bind the posted object that I want, but I don't know how else to describe the problem to get specific results that would lead me to my solution. Anything I search for leads to basic post and binding examples.
List inside the model I'm using:
public IList<_Result> Results get; set;
Class _Result has one of the properties:
public string Value get; set;
I fill up the list and use it in view like so:
@for (int i = 0; i < Model.Results.Count; i++)
...
<td>
<input asp-for="Results[i].Value" disabled />
</td>
...
I have checkboxes on form, which remove (with javascript) the "disabled" attribute from the inputs and thus allow them to be posted.
When I fill up the said list with 3 _Result objects, they are shown on form and all have the "disabled" attribute. If I remove the "disabled" attribute from the first two objects and click on submit button, I receive the Results list with first 2 _Result objects, which is as expected.
However, if I remove the "disabled" attribute only from the second _Result object (the first _Result object still has "disabled" attribute), the Results list comes back empty in my Controller method.
In my Form Data Header, I see this: "Results[1].Value: Value that I want posted", which means that post occurs, but list does not bind the object due to the index.
Any idea on how I can achieve that proper binding? Also, the reason I'm using "disabled" attribute is because I'm showing many results on a single page and want to only post those that are selected.
view asp.net-core-mvc model-binding
add a comment
|
Using "disabled" attribute on inputs on form does not post them, which is expected and wanted. However, if you prepare a form of 3 objects in a list, disable the first and third, and submit, the 2nd object appears in post header, but does not bind to the list correctly, because it has an index [1] instead of [0].
I understand how model binding works and why it does not bind the posted object that I want, but I don't know how else to describe the problem to get specific results that would lead me to my solution. Anything I search for leads to basic post and binding examples.
List inside the model I'm using:
public IList<_Result> Results get; set;
Class _Result has one of the properties:
public string Value get; set;
I fill up the list and use it in view like so:
@for (int i = 0; i < Model.Results.Count; i++)
...
<td>
<input asp-for="Results[i].Value" disabled />
</td>
...
I have checkboxes on form, which remove (with javascript) the "disabled" attribute from the inputs and thus allow them to be posted.
When I fill up the said list with 3 _Result objects, they are shown on form and all have the "disabled" attribute. If I remove the "disabled" attribute from the first two objects and click on submit button, I receive the Results list with first 2 _Result objects, which is as expected.
However, if I remove the "disabled" attribute only from the second _Result object (the first _Result object still has "disabled" attribute), the Results list comes back empty in my Controller method.
In my Form Data Header, I see this: "Results[1].Value: Value that I want posted", which means that post occurs, but list does not bind the object due to the index.
Any idea on how I can achieve that proper binding? Also, the reason I'm using "disabled" attribute is because I'm showing many results on a single page and want to only post those that are selected.
view asp.net-core-mvc model-binding
Using "disabled" attribute on inputs on form does not post them, which is expected and wanted. However, if you prepare a form of 3 objects in a list, disable the first and third, and submit, the 2nd object appears in post header, but does not bind to the list correctly, because it has an index [1] instead of [0].
I understand how model binding works and why it does not bind the posted object that I want, but I don't know how else to describe the problem to get specific results that would lead me to my solution. Anything I search for leads to basic post and binding examples.
List inside the model I'm using:
public IList<_Result> Results get; set;
Class _Result has one of the properties:
public string Value get; set;
I fill up the list and use it in view like so:
@for (int i = 0; i < Model.Results.Count; i++)
...
<td>
<input asp-for="Results[i].Value" disabled />
</td>
...
I have checkboxes on form, which remove (with javascript) the "disabled" attribute from the inputs and thus allow them to be posted.
When I fill up the said list with 3 _Result objects, they are shown on form and all have the "disabled" attribute. If I remove the "disabled" attribute from the first two objects and click on submit button, I receive the Results list with first 2 _Result objects, which is as expected.
However, if I remove the "disabled" attribute only from the second _Result object (the first _Result object still has "disabled" attribute), the Results list comes back empty in my Controller method.
In my Form Data Header, I see this: "Results[1].Value: Value that I want posted", which means that post occurs, but list does not bind the object due to the index.
Any idea on how I can achieve that proper binding? Also, the reason I'm using "disabled" attribute is because I'm showing many results on a single page and want to only post those that are selected.
view asp.net-core-mvc model-binding
view asp.net-core-mvc model-binding
asked Mar 28 at 10:14
DelyDely
34 bronze badges
34 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
For getting selected items, you could try checkbox with View Model instead of using jquery to control the disable property.
Change ViewModel
public class ModelBindVM
public IList<_ResultVM> Results get; set;
public class _ResultVM
public bool IsSelected get; set;
public string Value get; set;Controller
[HttpGet]
public IActionResult ModelBindTest()
ModelBindVM model = new ModelBindVM
Results = new List<_ResultVM>()
new _ResultVM Value = "T1" ,
new _ResultVM Value = "T2" ,
new _ResultVM Value = "T3"
;
return View(model);
[HttpPost]
public IActionResult ModelBindTest(ModelBindVM modelBind)
return View();View
<div class="row">
<div class="col-md-4">
<form asp-action="ModelBindTest">
@for (int i = 0; i < Model.Results.Count; i++)
<input type="checkbox" asp-for="Results[i].IsSelected" />
<label asp-for="@Model.Results[i].IsSelected">@Model.Results[i].Value</label>
<input type="hidden" asp-for="@Model.Results[i].Value" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
@Dely You could filtermodelBindby whetherIsSelectedis true.
– Tao Zhou
Mar 29 at 8:43
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
@Dely tryvar selected = modelBind.Results.Where(r => r.IsSelected).ToList();
– Tao Zhou
Apr 2 at 7:53
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
|
show 1 more comment
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/4.0/"u003ecc by-sa 4.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
);
);
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%2f55395021%2fhow-to-bind-a-list-of-objects-in-asp-net-core-mvc-when-the-posted-list-is-smalle%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For getting selected items, you could try checkbox with View Model instead of using jquery to control the disable property.
Change ViewModel
public class ModelBindVM
public IList<_ResultVM> Results get; set;
public class _ResultVM
public bool IsSelected get; set;
public string Value get; set;Controller
[HttpGet]
public IActionResult ModelBindTest()
ModelBindVM model = new ModelBindVM
Results = new List<_ResultVM>()
new _ResultVM Value = "T1" ,
new _ResultVM Value = "T2" ,
new _ResultVM Value = "T3"
;
return View(model);
[HttpPost]
public IActionResult ModelBindTest(ModelBindVM modelBind)
return View();View
<div class="row">
<div class="col-md-4">
<form asp-action="ModelBindTest">
@for (int i = 0; i < Model.Results.Count; i++)
<input type="checkbox" asp-for="Results[i].IsSelected" />
<label asp-for="@Model.Results[i].IsSelected">@Model.Results[i].Value</label>
<input type="hidden" asp-for="@Model.Results[i].Value" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
@Dely You could filtermodelBindby whetherIsSelectedis true.
– Tao Zhou
Mar 29 at 8:43
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
@Dely tryvar selected = modelBind.Results.Where(r => r.IsSelected).ToList();
– Tao Zhou
Apr 2 at 7:53
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
|
show 1 more comment
For getting selected items, you could try checkbox with View Model instead of using jquery to control the disable property.
Change ViewModel
public class ModelBindVM
public IList<_ResultVM> Results get; set;
public class _ResultVM
public bool IsSelected get; set;
public string Value get; set;Controller
[HttpGet]
public IActionResult ModelBindTest()
ModelBindVM model = new ModelBindVM
Results = new List<_ResultVM>()
new _ResultVM Value = "T1" ,
new _ResultVM Value = "T2" ,
new _ResultVM Value = "T3"
;
return View(model);
[HttpPost]
public IActionResult ModelBindTest(ModelBindVM modelBind)
return View();View
<div class="row">
<div class="col-md-4">
<form asp-action="ModelBindTest">
@for (int i = 0; i < Model.Results.Count; i++)
<input type="checkbox" asp-for="Results[i].IsSelected" />
<label asp-for="@Model.Results[i].IsSelected">@Model.Results[i].Value</label>
<input type="hidden" asp-for="@Model.Results[i].Value" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
@Dely You could filtermodelBindby whetherIsSelectedis true.
– Tao Zhou
Mar 29 at 8:43
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
@Dely tryvar selected = modelBind.Results.Where(r => r.IsSelected).ToList();
– Tao Zhou
Apr 2 at 7:53
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
|
show 1 more comment
For getting selected items, you could try checkbox with View Model instead of using jquery to control the disable property.
Change ViewModel
public class ModelBindVM
public IList<_ResultVM> Results get; set;
public class _ResultVM
public bool IsSelected get; set;
public string Value get; set;Controller
[HttpGet]
public IActionResult ModelBindTest()
ModelBindVM model = new ModelBindVM
Results = new List<_ResultVM>()
new _ResultVM Value = "T1" ,
new _ResultVM Value = "T2" ,
new _ResultVM Value = "T3"
;
return View(model);
[HttpPost]
public IActionResult ModelBindTest(ModelBindVM modelBind)
return View();View
<div class="row">
<div class="col-md-4">
<form asp-action="ModelBindTest">
@for (int i = 0; i < Model.Results.Count; i++)
<input type="checkbox" asp-for="Results[i].IsSelected" />
<label asp-for="@Model.Results[i].IsSelected">@Model.Results[i].Value</label>
<input type="hidden" asp-for="@Model.Results[i].Value" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
For getting selected items, you could try checkbox with View Model instead of using jquery to control the disable property.
Change ViewModel
public class ModelBindVM
public IList<_ResultVM> Results get; set;
public class _ResultVM
public bool IsSelected get; set;
public string Value get; set;Controller
[HttpGet]
public IActionResult ModelBindTest()
ModelBindVM model = new ModelBindVM
Results = new List<_ResultVM>()
new _ResultVM Value = "T1" ,
new _ResultVM Value = "T2" ,
new _ResultVM Value = "T3"
;
return View(model);
[HttpPost]
public IActionResult ModelBindTest(ModelBindVM modelBind)
return View();View
<div class="row">
<div class="col-md-4">
<form asp-action="ModelBindTest">
@for (int i = 0; i < Model.Results.Count; i++)
<input type="checkbox" asp-for="Results[i].IsSelected" />
<label asp-for="@Model.Results[i].IsSelected">@Model.Results[i].Value</label>
<input type="hidden" asp-for="@Model.Results[i].Value" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
answered Mar 29 at 2:34
Tao ZhouTao Zhou
11.4k3 gold badges18 silver badges37 bronze badges
11.4k3 gold badges18 silver badges37 bronze badges
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
@Dely You could filtermodelBindby whetherIsSelectedis true.
– Tao Zhou
Mar 29 at 8:43
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
@Dely tryvar selected = modelBind.Results.Where(r => r.IsSelected).ToList();
– Tao Zhou
Apr 2 at 7:53
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
|
show 1 more comment
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
@Dely You could filtermodelBindby whetherIsSelectedis true.
– Tao Zhou
Mar 29 at 8:43
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
@Dely tryvar selected = modelBind.Results.Where(r => r.IsSelected).ToList();
– Tao Zhou
Apr 2 at 7:53
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
Doing it this way, it posts "IsSelected" property too. If I had 1000 results showing, this would make it a 2000 items post. What I'm trying to achieve is that if I'm showing 1000 results and only 5 are selected, I want to post only those 5 (and bind them correctly).
– Dely
Mar 29 at 7:32
@Dely You could filter
modelBind by whether IsSelected is true.– Tao Zhou
Mar 29 at 8:43
@Dely You could filter
modelBind by whether IsSelected is true.– Tao Zhou
Mar 29 at 8:43
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
How can I do that? Could you point me in the right direction?
– Dely
Apr 2 at 7:49
@Dely try
var selected = modelBind.Results.Where(r => r.IsSelected).ToList();– Tao Zhou
Apr 2 at 7:53
@Dely try
var selected = modelBind.Results.Where(r => r.IsSelected).ToList();– Tao Zhou
Apr 2 at 7:53
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
Okay, but that is filtering results that were already bound. If I check in Headers->Form Data in dev tools in browser, I still post all of the results (which are many). My goal is to post only those results that are selected, not filter them after they were posted.
– Dely
Apr 2 at 8:11
|
show 1 more comment
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
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%2f55395021%2fhow-to-bind-a-list-of-objects-in-asp-net-core-mvc-when-the-posted-list-is-smalle%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