My issue involves updating an entity via an edit form with a reference to another entity represented by a drop down list. I'm using ASP.NET MVC 2. Details:
Public Class Category
{
int Id { get; set;}
string Name { get; set}
Category Parent { get; set}
}
- An edit page for the category generated by
EditorFor
- Edit page contains a drop down for selecting the parent category, with name/id=
Parent.Id
- There is a 'none' entry in the drop down list with
value=''
(for categories with no parent).
Update process in Action:
- Current entity retrieved from repository by id.
- TryUpdateModel applied to retrieved entity
Problem:
When an edited category is submitted, if the dropdown is set to 'none' a new entity is being instantiated for 'parent' when it tries to update on the parent.id. This causes problems when persisting via the ORM. So, what to do in this situation? Is there any way to stop the 'parent' object from being instantiated, and leaving the parent reference null?
Thanks.
Update: I'm using NHibernate as my ORM, in case this is useful.