MVC conserva una colección mixta de registros nuevos y existentes

Usando el método de Blog de Steve Sanderson, 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.

preguntado el 11 de junio de 12 a las 19:06

1 Respuestas

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.

Respondido el 14 de junio de 12 a las 13:06

Actually - at the risk of doing needless updates session.SaveOrUpdate(object obj) will do exactly what it says on the tin. You can leave it up to Nhibernate to decide as to whether it needs to be saved/inserted or updated. - Pat Hastings

Yes, the id's are generated by nhibernate. The call to the repository is fixed to being just an Update (according to the specs I must follow). So, in lieu of calling SaveOrUpdate, I'll just have to check its null/0 status first. Thanks Patrick. - getit

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.