0

I am using MVC4 C# Razor view and MS SQL Server. I need to insert a list/array value from controller to sql server. I am passing values from view to controller and getting the values in controller.

My data structures are -

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

Above data needed to insert my table (tbl_monthqty) in the below order. ID auto generated -

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

If any value null or 0, need to ignore

MonthID is for example - m1Qty = 1, m2Qty = 2, m3Qty = 3

My controller (C#) is -

[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 });
}

My class is -

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

I am getting the list value in controller but not sure how I will insert those list/array values in my table. I have tried few asked questions like this but did not work.

13

1 Answer 1

2

First create data model that represent json data structure:

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

Then data model that you want to store the data:

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

Then convert the json to list of FirstModel objects (I assume you already did it), and finally convert data in List to 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);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.