I have an Object
public class Object1{
public List<Object2> {get;set;}
}
public class Object2{
public Name{get;set;}
public Address{get;set;}
}
I have a feature where the user can update just one instance of Object2. So my code for saving Object2 looks like
[HttpPost]
public ActionResult SaveObject2(Object2 obj2)
{
if (obj2.Id == null){
//Add Logic
obj1.Obj2List.Add(obj2)
}
else{
// Update logic
}
}
But obj2.Id is never null.Id is of type ObjectId. How can i check for logic to see if need to insert or update ? I am using asp.net MVC 3 and Mongo DB using the official C# drivers.
Thanks
The
ObjectIdtype is a struct, not a class – so it’s never going to benull. Check forObjectId.Empty, instead.A word of caution, though: I suppose that you are storing the id of
Object2in some hidden field between requests. If that is the case, be aware that malicious user can easily change the ID by using an HTTP proxy (such as Fiddler), thus tricking you into believing that theObject2is being updated instead of added.Depending on context of what you are trying to do, I would suggest performing some additional checks to more reliably determine if you should insert or update your object.