我正在努力确定这只是一个设计问题和/或我如何从编码角度解决以下场景:
在我的项目中,我允许用户添加多个送货地址。显然,其中一个应该是送货地址。但是,我想根据用户的意愿提供更改此功能的可能性。 因此,我使用名为“IsDefaultShippingAddress”(bool)的列存储了送货地址。我想到的是,当用户选择送货地址作为默认设置时,模型会为该列传递“true”。
现在,在用户具有选定为默认的现有送货地址并且想要添加新的默认送货地址(或将现有送货地址提升为默认)的情况下,我自然会在同一用户在同一个表中有两个(多个)记录的情况,其中'IsDefaultShippingAddress'为'true' - 但我希望它只是一个地址(最新提升的)根据术语'default'的性质。
因此我的问题是,如何设置符合特定条件的所有记录(UserID)更新为'IsDefaultShippingAddress' 'false'
,同时设置新升级的送货地址(同一表中的单独行)到'IsDefaultShippingAddress' 'true'
?
这是我的控制器代码,问题尚未解决:
// 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);
}
答案 0 :(得分:1)
将此代码放在if语句之前。
我还没有执行代码。可能存在一些语法错误但你明白了。
if (model.IsDefaultShippingAddress)
{
var addresses = DATADB.ShippingAddresses.Where(address => address.UserId == userId);
addresses.ForEach(address => address.IsDefaultShippingAddress = false);
}
DATADB.SaveChanges();
答案 1 :(得分:0)
谢谢Selçuk - 在你的帮助下,我走上了正确的道路。我几乎没有曲折,现在我有了解决方案:
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)
{
if (model.IsDefaultShippingAddress)
{
var addresses = DATADB.ShippingAddressList
.Where(a => a.UserID == userID);
var address = DATADB.ShippingAddressList
.Where(a => a.ID == model.ID);
addresses.ForEach(a => a.IsDefaultShippingAddress = false);
address.ForEach(a => a.IsDefaultShippingAddress = model.IsDefaultShippingAddress);
address.ForEach(a => a.ShippingAddressCompanyName = model.ShippingAddressCompanyName);
address.ForEach(a => a.ShippingAddressFirstName = model.ShippingAddressFirstName);
address.ForEach(a => a.ShippingAddressLastName = model.ShippingAddressLastName);
address.ForEach(a => a.ShippingAddressAdditional = model.ShippingAddressAdditional);
address.ForEach(a => a.ShippingAddressStreet = model.ShippingAddressStreet);
address.ForEach(a => a.ShippingAddressNumber = model.ShippingAddressNumber);
address.ForEach(a => a.ShippingAddressZIP = model.ShippingAddressZIP);
address.ForEach(a => a.ShippingAddressCity = model.ShippingAddressCity);
address.ForEach(a => a.ShippingAddressState = model.ShippingAddressState);
address.ForEach(a => a.ShippingAddressCountry = model.ShippingAddressCountry);
address.ForEach(a => a.ShippingInstructions = model.ShippingInstructions);
}
DATADB.SaveChanges();
// track user activity: post method includes activity name and timestamp along with location
var SUCCESS = new UserActivities
{
UserID = userID,
ActivityName = "EditShippingAddress_Success",
ActivityTimeStampUTC = nowUTC,
ActivityLatitude = model.UpdatedLatitude,
ActivityLongitude = model.UpdatedLongitude,
ActivityLocation = model.UpdatedLongitude
};
DATADB.UserActivityList.Add(SUCCESS);
DATADB.SaveChanges();
return RedirectToAction("ShippingAddresses");
}
var FAILURE = new UserActivities
{
UserID = userID,
ActivityName = "EditShippingAddress_Failure",
ActivityTimeStampUTC = nowUTC,
ActivityLatitude = model.UpdatedLatitude,
ActivityLongitude = model.UpdatedLongitude,
ActivityLocation = model.UpdatedLongitude
};
DATADB.UserActivityList.Add(FAILURE);
DATADB.SaveChanges();
return View(model);
}