MVC 4 - 将 list/array 从控制器插入到 SQL 服务器

MVC 4 - Insert list/array from Controller to SQL Server

我正在使用 MVC4 C# Razor 视图和 MS SQL 服务器。我需要从控制器插入一个 list/array 值到 sql 服务器。我将值从视图传递到控制器并在控制器中获取值。

我的数据结构是-

{sid: "101", m1Qty: "1", m2Qty: "3", m3Qty: ""}
{sid: "102", m1Qty: "5", m2Qty: "6", m3Qty: ""}
{sid: "103", m1Qty: "8", m2Qty: "0", m3Qty: ""}

以上数据需要按以下顺序插入我的 table (tbl_monthqty)。 ID 自动生成 -

ID  SID     MonthID   mQty
1   101        1       1 
2   102        1       5
3   103        1       8
4   101        2       3
5   102        2       6

如果有任何值null0,需要忽略

MonthID 例如 - m1Qty = 1, m2Qty = 2, m3Qty = 3

我的控制器 (C#) 是 -

[HttpPost]
public JsonResult SaveQty(IList<AllQty> model)
{
    var list = new [] { model };
    var count = list.Count();

    DataTable dt = new DataTable();
    dt.Columns.Add("SID");
    dt.Columns.Add("MonthID");
    dt.Columns.Add("mQty");

    for(int i=0; i<count; i++)
    {
        //dt.Rows.Add();
        // Not sure what I will do here
    }

    return Json(new { success = true });
}

我的class是-

public class AllQty
{
    public int SID { get; set; }
    public int MonthID { get; set; }
    public int mQty { get; set; }
} 

我正在控制器中获取列表值,但不确定如何将这些 list/array 值插入我的 table。我已经尝试了几个像 this 这样的问题,但没有成功。

首先创建表示 json 数据结构的数据模型:

public class FirstModel
  {
    public int SID;
    public string m1Qty;
    public string m2Qty;
    public string m3Qty;
  }

那么你要存储数据的数据模型:

public class AllQty
  {
    public int SID { get; set; }
    public int MonthID { get; set; }
    public int mQty { get; set; }
  } 

然后将 json 转换为 FirstModel 对象列表(我假设您已经这样做了),最后将 List 中的数据转换为 List :

        List<FirstModel> qtYs = new List<FirstModel>();
        List<AllQty> allQties = new List<AllQty>();
        foreach (FirstModel item in qtYs)
        {
            if (string.IsNullOrEmpty(item.m1Qty))
            {
                AllQty allQty = new AllQty
                {
                    MonthID = 1,
                    mQty = int.Parse(item.m1Qty),
                    SID = item.SID
                };
                allQties.Add(allQty);
            }

            if (string.IsNullOrEmpty(item.m2Qty))
            {
                AllQty allQty = new AllQty
                {
                    MonthID = 2,
                    mQty = int.Parse(item.m1Qty),
                    SID = item.SID
                };
                allQties.Add(allQty);
            }

            if (string.IsNullOrEmpty(item.m3Qty))
            {
                AllQty allQty = new AllQty
                {
                    MonthID = 3,
                    mQty = int.Parse(item.m1Qty),
                    SID = item.SID
                };
                allQties.Add(allQty);
            }
        }

        DataTable dt = new DataTable();
        dt.Columns.Add("SID");
        dt.Columns.Add("MonthID");
        dt.Columns.Add("mQty");

        foreach (AllQty allQty in allQties)
        {
            var row = dt.NewRow();

            row["SID"] = allQty.SID;
            row["MonthID"] = allQty.MonthID;
            row["mQty"] = allQty.mQty;

            dt.Rows.Add(row);
        }