I am struggling a bit to define whether this is just a design issue and/or how I could address the following scenario from a coding angle:
In my project, I allow a user to add multiple shipping addresses. Obviously, one of these is supposed to be the shipping address. However, I want to offer the possibility to change this at the user's discretion. I have therefore stored shipping addresses with a column called 'IsDefaultShippingAddress' (bool). What I have in mind doing is, when a user selects a shipping address to be the default, the model would pass on 'true' for that column.
Now, in a scenario where a user has an existing shipping address which is selected as default, and would like to add a new default shipping address (or promote an existing shipping address to default), I would naturally end up in a scenario where the same user has two (multiple) records in the same table where 'IsDefaultShippingAddress' is 'true' - but I want it to be only a single address (the latest promoted one) as per nature of the term 'default'.
Therefore my question is, how can I make sure that all records fitting a certain criteria (UserID) are updated to 'IsDefaultShippingAddress' 'false' while the newly promoted shipping address (separate row in same table) is being set to 'IsDefaultShippingAddress' 'true'?
Here's my controller code, which is the one where the problem is unsolved:
// POST: /Manage/EditShippingAddress
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditShippingAddress([Bind(Include = "ID,UserID,IsDefaultShippingAddress,ShippingAddressCompanyName,ShippingAddressFirstName,ShippingAddressLastName,ShippingAddressAdditional,ShippingAddressStreet,ShippingAddressNumber,ShippingAddressZIP,ShippingAddressCity,ShippingAddressState,ShippingAddressCountry,ShippingInstructions,UpdatedLatitude,UpdatedLongitude,UpdatedLocation")] ShippingAddresses model)
{
// define variables
var userID = User.Identity.GetUserId();
DateTime nowUTC = DateTime.Now.ToUniversalTime();
DateTime nowLocal = DateTime.Now.ToLocalTime();
// pass first name to viewbag for personalization
ViewBag.Personalization = UserManager.FindById(userID).FirstName.ToString();
if (ModelState.IsValid)
{
DATADB.Entry(model).State = System.Data.Entity.EntityState.Modified;
DATADB.SaveChanges();
return RedirectToAction("ShippingAddresses");
}
return View(model);
}