Very often there is a need to add items in a collection of items. Usually we have some object that holds a list of complex objects, for example, on...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Stevan,
After also fighting a while against the model binder and understanding what it is necessary for it to work properly, I decided to write a library for creating dynamic lists in ASP.NET. I wanted to share it here in case it would be useful for you and the others in the comments. I've called it Dynamic View-Model Lists (very creative, I know) and it's available at dynamic-vml.github.io/
In a particular application I was working on, I had to create several lists for different models using slightly different layouts to use in wizard-like pages. So I decided to make the library a templating engine where you could easily define different view templates for each component of a dynamic list. It also supports nesting lists with any depth.
It comes with sample applications if anyone would like to see examples that just work out-of-the-box.
Hope someone finds it useful!
Cesar
Hi César,
I had a couple issues with Stevan's solution so I just came across your library and decided to give it a shot. After installing it, for some reason " using DynamicVML;" is not resolving, although it does appear under Packages! Do you know what the issue could be? Am I supposed to reference it somewhere else before referencing it in my ViewModel? Awaiting your response, thanks!
So, adding the item the first time works but if I try to add another item, it doesn't post back the first item that was added so there's nothing to add to. It's acting as if the form isn't including the new items added in the partial
I can't reproduce that, can you check out my repository
github.com/stevcooo/AddItemsDynami...
Yes will do. I've been working on this exact scenario for a while now and I'm getting really frustrated with the inconsistency in how the model binder works... I think I've gotten to the point where I can consistently add more items, but I'm noticing that the post back of new items appears in the payload but not in the page model. But the page model has the other data and the payload does not.
Ok I was able to get rid of it. I was using razor pages instead of controllers and I had a javascript bug. So I can add all day long. Can you expand this to add a delete? I'm seeing an issue where the form data doesn't seem to be updating. To reproduce, add two items an keep track of their IDs. Delete the first item and everything seems fine, now add one back, suddenly the first ID is back
Thank you for the feedback, I'll check this out, if that's the case on my side too, I'll fix it and I let you know.
Heey, Great explanation! But I am having a little trouble. The @Html.EditorFor() is not working for me. It does not load the partial view what so ever. I think I did everything the same way you explain it. Any help you could offer?
Just add the model template you are trying to add with EditorFor() to the ~Views/Shared/EditorTemplates/YOURPARTIALVIEW.cshtml.
Create EditorTemplates if it doesn't exist!
Thanks for your reply, but I already have that. So that is not the fix.
Hi! I am having the same issue, did you figure out what the issue may be?
Stevan wonderfull job.Thanks for sharing.It is uniqe on the net.
Thanks again
Creating my first MVC application, however I've gotten stuck a bit at the end. You reference a '_context' object, and yet in mine I don't seem able to access that. Is it an inherited member of the Controller, am I missing a reference, or did you construct it in some other tutorial?
Hello, Stevan, thank you for your tutorial. It is just great, it is in a big help for me in my first MVC project. I would like to ask you something additional about the PartialViews loaded inside the form.
I have a Partial View with a JavaScript inside which has to operate only on the exact view but instead this script only operates on the first generated partial view. Do you have an idea how to mend this?
I don't know what is your javascript code in the partial view, it's hard for me to give you an idea or solution. Maybe you can consider making a more general javascript code and run it on the view, that way you will avoid loading javascript with all your partial views.
Great article, and exactly what I was after, but I am having an issue with part of my data coming in as null in the AddOrderItem controller method.
If you were to add in a complex data type into OrderItem (i.e IFormFile, or another Custom object), would you need to create a custom model binder to get the data in?
Thanks for posting this, it is really helpful. but I got a question...if we want to delete an item dynamically after we added some, how would you accomplish that?
Great job! Exactly what I was looking for.
What about deleting them dynamically?