Using the method from Steve Sanderson’s blog, I have a list of items that I can dynamically add to with jquery.
So, I have a list of vehicles (that belong to a Person) that I load from the DB. When I hit save, the items get binded via the standard MVC model binding mechanism, and I can just call an Update on the the Person object.
The problem exists when I dynamically add a new vehicle row. Since that vehicle did not belong to the initial list, calling Update won’t persist that new item. But, calling Create won’t work because I have the existing vehicles that need updating.
Is there an elegant way of handling this, or do I need to loop through each vehicle, check if it exists in the DB and then call Update/Edit depending on the result?
I am using NHibernate with C# in MVC3.
What method are you using for generating the ids of your entities?
If they are being generated by nhibernate can you not just check the Id for each vehicle –
If it is null or default (guid.empty, 0 etc depending on what the type of your Id) then it has never been near your db and needs to be inserted.
If it has an Id then it already exists in your db and needs to be updated.