在MVC中获取多个嵌套的局部视图

时间:2018-11-28 07:25:34

标签: asp.net-mvc

我有一页,其中包含一个多个部分页面。在每个局部视图中,将再次有多个局部页面。当我得到这个时,它将添加数据表到包含三个表的ds中。如何按层次结构显示它?

在我的ds中,来自数据库的三个表显示了特定的记录。我该如何显示?

这是我的DAL:

 public static ConsignmentNote GetConsignmentNoteByID(int id)
        {
            ConsignmentNote consignmentnote = null;
            SqlConnection sqlConnection = null;
            sqlConnection = SqlConnectionHelper.GetConnectionConnectionString();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            try
            {
                cmd = new SqlCommand("GetConsignmentNoteByCNNoteId", sqlConnection);
                cmd.Parameters.Add(new SqlParameter("@ConsignmentNoteID", id));
                cmd.CommandType = CommandType.StoredProcedure;
                da.SelectCommand = cmd;
                da.Fill(ds);

                //First table data
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    consignmentnote = new ConsignmentNote(row);
                }
                else
                {
                    consignmentnote = new ConsignmentNote();
                }

                //Second table data 
                consignmentnote.LstAdditionalCN = new List<AdditionalCN>();
                int rowIndexCN = 1;

                if (ds.Tables[1].Rows.Count > 0)
                {
                    int i = 0;
                    foreach (DataRow acnRow in ds.Tables[i].Rows)
                    {                       

                        AdditionalCN objACN = new AdditionalCN();

                        objACN.ConsignmentNoteRelID = acnRow["ConsignmentNoteRelID"] as Int32? ?? 0;
                        objACN.ConsignmentNoteID = acnRow["ConsignmentNoteID"] as Int32? ?? 0;
                        objACN.ConsignmentNoteNumber = acnRow["ConsignmentNoteNumber"] as string ?? null;
                        objACN.CNDate = acnRow["CNDate"] as DateTime? ?? DateTime.MinValue;
                        objACN.ConsignerID = (acnRow["ConsignerID"] as Int32? ?? 0).ToString();
                        objACN.ConsigneeID = (acnRow["ConsigneeID"] as Int32? ?? 0).ToString();
                        objACN.LocationFrom = acnRow["LocationFrom"] as string ?? null;
                        objACN.LocationTo = acnRow["LocationTo"] as string ?? null;

                        if (rowIndexCN == 1)
                        {
                            //insert to default CNI
                            consignmentnote.ConsignmentNoteRelID = objACN.ConsignmentNoteRelID;
                            consignmentnote.ConsignmentNoteNumber = objACN.ConsignmentNoteNumber;
                        }
                        else
                        {
                            //insert to additional CNI
                            consignmentnote.LstAdditionalCN.Add(objACN);
                        }
                        rowIndexCN++;
                    }
                }

                consignmentnote.LstAdditionalInvoice = new List<AdditionalInvoice>();
                int rowIndexCNInvoice = 1;

                if (ds.Tables[2].Rows.Count > 0)
                {
                    foreach (DataRow acnRow in ds.Tables[2].Rows)
                    {
                        AdditionalInvoice objACN = new AdditionalInvoice();

                        objACN.ConsignmentNoteLineItemID = acnRow["ConsignmentNoteLineItemID"] as Int32? ?? 0;
                        objACN.ConsignmentNoteID = acnRow["ConsignmentNoteID"] as Int32? ?? 0;
                        objACN.ConsignmentNoteRelID = acnRow["ConsignmentNoteRelID"] as Int32? ?? 0;
                        objACN.ProjectPO = (acnRow["ProjectPO"] as Int32? ?? 0).ToString();
                        objACN.InvoiceNum = (acnRow["InvoiceNum"] as Int32? ?? 0).ToString();
                        objACN.InvoiceDate = acnRow["InvoiceDate"] as DateTime? ?? DateTime.MinValue;
                        objACN.Pkgs = acnRow["Pkgs"] as string ?? null;
                        objACN.Description = acnRow["Description"] as string ?? null;

                        objACN.ActualWeight = acnRow["ActualWeight"] as string ?? null;
                        objACN.ChargedWeight = acnRow["ChargedWeight"] as string ?? null;
                        objACN.InvoiceValue = acnRow["InvoiceValue"] as decimal? ?? 0;

                        if (rowIndexCNInvoice == 1)
                        {
                            //insert to default CNI
                            consignmentnote.ConsignmentNoteRelID = objACN.ConsignmentNoteRelID;
                            consignmentnote.ConsignmentNoteID = objACN.ConsignmentNoteID;
                        }
                        else
                        {
                            //insert to additional CNI
                            consignmentnote.LstAdditionalInvoice.Add(objACN);
                        }
                        rowIndexCNInvoice++;
                    }
                }

            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {

            }
            return consignmentnote;
        }

0 个答案:

没有答案