正在尝试根据表单外的条目将信息输入数据库列
本文关键字:信息 输入 数据库 表单 | 更新日期: 2023-09-27 17:59:27
我甚至不知道这个问题该怎么说,所以请耐心等待。如果需要,我会编辑。
让我们从我的代码开始:
var assetMonth = "assest."+Month;
var billableAssests = (from s in db.Assets
where s.SystemPosition == 1
select s).ToList();
try {
foreach (var assests in billableAssests)
{
assests.PreviousDateBilled = assests.LastDateBilled;
assests.LastDateBilled = today;
assetMonth = assests.MonthlyChargeRate; <===HERE
assests.RunnungLeaseTotal = assests.RunnungLeaseTotal + assests.MonthlyChargeRate;
if (assests.MonthBilled == null)
{
assests.MonthBilled = 1;
}
else
{
assests.MonthBilled = assests.MonthBilled + 1;
}
//db.Entry(billableAssests).State = EntityState.Modified;
db.SaveChanges();
}
}
我正在从用户那里获得他们想要计费的月份,并希望将每月收费率插入该月份列中。我一辈子都想不出该怎么做。有什么建议吗?或者告诉我从哪里开始找?
谢谢!
编辑***表格模型
public class Asset
{
[Key]
public string AssetTag { get; set; }
public string SerialNumber { get; set; }
public int WarrantyPeriod { get; set; }
public string DeviceApprover { get; set; }
public int Dept_id { get; set; }
public string AssetLocation { get; set; }
public string DeliveryLocation { get; set; }
public int SectionKey { get; set; }
public int VendorKey { get; set; }
public int DeviceInformationKey { get; set; }
public int EmployeeKey { get; set; }
public decimal PurchasePrice { get; set; }
public decimal? BaseLeaseRate { get; set; }
public decimal? MonthlyChargeRate { get; set; }
public decimal? LeaseTotal { get; set; }
public int? MonthBilled { get; set; }
public DateTime? LeaseStartDate { get; set; }
public DateTime? LeaseEndDate { get; set; }
public decimal? RunnungLeaseTotal { get; set; }
public int deliveryEmployeeKey { get; set; }
public DateTime? InstallDate { get; set; }
public Boolean? Confirm { get; set; }
public string Status { get; set; }
public int? SystemPosition { get; set; }
public DateTime? LastDateBilled { get; set; }
public DateTime? PreviousDateBilled { get; set; }
public DateTime? ReturnedDate { get; set; }
public int Account { get; set; }
public decimal? Jan { get; set; }
public decimal? Feb { get; set; }
public decimal? Mar { get; set; }
public decimal? Apr { get; set; }
public decimal? May { get; set; }
public decimal? June { get; set; }
public decimal? July { get; set; }
public decimal? Aug { get; set; }
public decimal? Sept { get; set; }
public decimal? Oct { get; set; }
public decimal? Nov { get; set; }
public decimal? Dec { get; set; }
public virtual Section Section { get; set; }
public virtual Vendor Vendor { get; set; }
public virtual DeviceInformation DeviceInformation { get; set; }
public virtual Employee Employee { get; set; }
好吧,您有几个选项。
第一种方法是重构你的模型,可能还有你的数据库,这样你就有了一个月值的集合,而不是在12个月内有12个字段。这将更容易更新,因为您可以使用传入的月份来确定您想要的条目(如果存在),或者添加它(如果不存在)。
public class AssetMonth
{
string AssetTag { get;set;}
DateTime Month { get;set;} // DateTime would *probably* be the best choice for this
Decimal? MonthlyChargeRate { get;set;}
}
第二个是使用一个好的老式switch
语句:
switch(Month) // I don't know what type this is; I'm assuming string.
{
case "Jan":
model.Jan = assets.MonthlyChargeRate;
break;
case "Feb":
model.Feb = assets.MonthlyChargeRate;
// etc.
}
第三,如果你真的很喜欢,你可以用反思来设定价值。
PropertyInfo prop = typeof(Asset).GetProperty(Month); // again, assuming string
prop.SetValue(model, assets.MonthlyChargeRage);
但我鼓励您重构模型。这将为您提供最佳设计,并消除对解决方案(如选项2和选项3)的需求。