2013-02-15 52 views
0

我甚至不知道如何说出这个问题,请耐心等待。如果需要,我会编辑。试图根据表单条目输入信息到数据库列

让我们开始与我的代码:

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; } 
+0

你能告诉我们你想插入的表是什么样子吗? – 2013-02-15 16:31:06

+0

是的@AnnL。我认为我们需要看看资产表(但我可能是错的)。您需要向我们展示具有您希望我们插入的列的表格/实体 – 2013-02-15 16:34:00

+0

该模型可以做什么? – mdarling 2013-02-15 16:36:49

回答

2

井模型,你有两个选择。

首先是重构你的模型和可能的数据库,以便有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这样的解决办法的需要。

+0

你打败了我! – 2013-02-15 18:50:34

+0

谢谢。懒惰的我不想重构模型。但这是处理这个问题的正确方法。 – mdarling 2013-02-19 12:47:17

相关问题