MVC在模型到ViewModel映射后将值插入到ViewModel中

时间:2018-11-07 19:11:46

标签: asp.net-mvc-5 oledb viewmodel

我正在使用两个不同的数据库。我将模型集合(SQL Server)传递给ViewModel集合。 ViewModel具有我可以从Visual Fox Pro数据库中访问的其他属性。我可以映射现有属性,但是ViewModel在将值传递给它之后不保存数据。

WoCustLname字段返回null,但是来自原始Model的其余属性将传递给ViewModel中的属性。

当我在rdr调试OleDbCommand时,它表明ViewModel正在接收rdr[WoCust]rdr[Lname]的值。

如何做到这一点,以便ViewModel保存新值?

WOSchedule.cs ...

public partial class WOSchedule
{
    public long Id { get; set; }
    public string WoNo { get; set; }
    public Nullable<long> QuoteTypeId { get; set; }
    public Nullable<int> PriorityNo { get; set; }
    public bool Active { get; set; }
    public Nullable<System.DateTime> WoDate { get; set; }
    public Nullable<long> QuoteID { get; set; }
    public Nullable<System.DateTime> WoDone { get; set; }
    public Nullable<long> WOScheduleListId { get; set; }
    public string StorageLocation { get; set; }

    public virtual QuoteType QuoteType { get; set; }
    public virtual Quote Quote { get; set; }
    public virtual WOScheduleList WOScheduleList { get; set; }
}

WoWcheduleVM.cs ...

public partial class WoScheduleVM
{
    public long Id { get; set; }
    public string WoNo { get; set; }
    public Nullable<long> QuoteTypeId { get; set; }
    public Nullable<int> PriorityNo { get; set; }
    public bool Active { get; set; }
    public DateTime? WoDate { get; set; }
    public Nullable<long> QuoteID { get; set; }
    public DateTime? WoDone { get; set; }
    public Nullable<long> WOScheduleListId { get; set; }
    public string StorageLocation { get; set; }

    public string WoCust { get; set; } // extra property
    public string Lname { get; set; } // extra property

    public virtual QuoteType QuoteType { get; set; }
    public virtual Quote Quote { get; set; }
    public virtual WOScheduleList WOScheduleList { get; set; }
}

WOSchedulesController.cs

string cs = ConfigurationManager.ConnectionStrings["foxproTables"].ConnectionString;
OleDbConnection cn = new OleDbConnection(cs);
var wOSchedules = db.WOSchedules.Where(w => w.WoDone == null).Include(w => w.QuoteType);

var wOSchedulesVM = wOSchedules.Select(s => new ViewModels.WoScheduleVM()
  {
     Id = s.Id,
     WoNo = s.WoNo,
     QuoteTypeId = s.QuoteTypeId,
     PriorityNo = s.PriorityNo,
     Active = s.Active,
     WoDate = s.WoDate,
     QuoteID = s.QuoteID,
     WoDone = s.WoDone,
     WOScheduleListId = s.WOScheduleListId,
     StorageLocation = s.StorageLocation
   });

cn.Open();
foreach (var sch in wOSchedulesVM)
  {
    string conn = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = '" + sch.WoNo + "'";
    OleDbCommand cmdWO = new OleDbCommand(conn, cn);
    OleDbDataReader rdr = cmdWO.ExecuteReader();
    while (rdr.Read())
    {
      sch.WoCust = ((string)rdr["wo_cust"]).Trim();
      sch.Lname = ((string)rdr["lname"]).Trim();
    }
  }
cn.Close();

return View(wOSchedulesVM.OrderByDescending(d => d.WoDate).ToList());

1 个答案:

答案 0 :(得分:0)

问题是您使用foreach循环来迭代wOSchedulesVM集合,这使得源集合在迭代过程中不可变。 older documentation version明确解释了该行为:

  

foreach语句用于遍历集合以获取   所需的信息,但不能用于添加或删除   源集合中的项,以避免产生不可预测的副作用。如果需要从源集合中添加或删除项目,请使用for循环。

因此,应该使用for循环才能修改该集合内的属性值,如下例所示:

using (var OleDbConnection cn = new OleDbConnection(cs))
{
    cn.Open();
    string cmd = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = @WoNo";

    // not sure if it's 'Count' property or 'Count()' method, depending on collection type
    for (int i = 0; i < wOSchedulesVM.Count; i++)
    {
        var sch = wOSchedulesVM[i];
        using (OleDbCommand cmdWO = new OleDbCommand(conn, cn))
        {
            cmd.Parameters.AddWithValue("@WoNo", sch.WoNo)
            OleDbDataReader rdr = cmdWO.ExecuteReader();
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    sch.WoCust = (!rdr.IsDbNull(0)) ? rdr.GetString(0).Trim() : string.Empty;
                    sch.Lname = (!rdr.IsDbNull(1)) ? rdr.GetString(1).Trim() : string.Empty;
                }
            }
        }
    }
}

注意:此示例还包括3个方面,即参数化查询,使用HasRows属性检查行是否存在以及使用DBNull.Value检查IsDbNull()。 >

相关问题:What is the best way to modify a list in a 'foreach' loop?