Use of ICollection in ASP.Net MVC

Last Reply 6 months ago By dharmendr

Posted 6 months ago

What is the use of this line :

public virtual ICollection<PurchasedItem> PurchasedItems { get; set; }

How to properly assign my getPur() method to Transaction.PurchasedItems I need a resultset which is a combination of Transaction and PurchasedItems

I'm looking for the result same like in image. Right now I'm getting the desired result with making a new model which contains all the columns. but is it possible to this without making a new model.

Kindly guide me to make this compact.

Models:

    public class Transaction
    {
        public int TransactionId { get; set; }

        public string Salesman { get; set; }
        public Nullable<int> SalesmanId { get; set; }
        public string voucherno { get; set; }

        public string Progress { get; set; }
        public int ProgressId { get; set; }

        public string Status { get; set; }
        public int StatusId { get; set; }

        public decimal Net { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime Date_ { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> Date2_ { get; set; }
        public string AppointmentRemarks { get; set; }

        public int CustomerId { get; set; }

        public virtual ICollection<PurchasedItem> PurchasedItems { get; set; }
    }
    public class PurchasedItem
    {
        public int CustomerId { get; set; }
        public int ProgressId { get; set; }
        public string Product { get; set; }
        public decimal Qty { get; set; }
        public decimal Price { get; set; }
        public decimal Disc { get; set; }
        public decimal Total { get; set; }
        public string ProductRemarks { get; set; }
    }

View Model:

public class CustomerViewModel
{
    public Transaction Transaction { get; set; }
    public IEnumerable<Transaction> Transactions { get; set; }
    public IEnumerable<PurchasedItem> PurchasedItems { get; set; }
    public PurchasedItem PurchasedItem { get; set; }
}

Controller:

        public ActionResult History(int? id)
        {
            CustomerViewModel VM = new CustomerViewModel();
            VM.Transactions = getTransactions().Where(t => t.CustomerId == id);
            VM.Transaction.PurchasedItems = getPur().ToList().Where(t => t.CustomerId == id).ToList();

            return View(VM);
        }

        public IEnumerable<PurchasedItem> getPur()
        {
            var prod = (from c in db.Customers.Include(c => c.CUSTOMERTYPE)
                        join ch in db.CUSTOMERHEADs.Include(ch => ch.CustomerDatas).Include(ch => ch.Customer).Include(ch => ch.CustomerProgress).Include(ch => ch.CustomerStatu) on c.Id equals ch.CustomerId into l2
                        from ch in l2.DefaultIfEmpty()
                        join sm in db.SalesMen on ch.SalesManId equals sm.MasterId into l1
                        from sm in l1.DefaultIfEmpty()
                        join cd in db.CustomerDatas on ch.id equals cd.Headid into l3
                        from cd in l3.DefaultIfEmpty()
                        select new
                        {
                            cid = c.Id,
                            prod = cd.ProductCode,
                            price = cd.Price,
                            qty = cd.Qty,
                            disc = cd.Disc,
                            amount = cd.Amount,
                            remarks = cd.Remarks,
                            progressid = ch.CustomerProgress == null ? 0 : ch.CustomerProgress.Id,

                        });
            var details = new List<PurchasedItem>();
            foreach (var t in prod)
            {
                details.Add(new PurchasedItem()
                {
                    CustomerId = t.cid,
                    ProgressId = t.progressid,
                    Product = t.prod,
                    Qty = Convert.ToDecimal(t.qty),
                    Price = Convert.ToDecimal(t.price),
                    Disc = Convert.ToDecimal(t.disc),
                    Total = Convert.ToDecimal(t.amount),
                    ProductRemarks = t.remarks,

                });
            }
            return details;
        }

View:

@model CustomerViewModel

<fieldset class="Block">
        <legend>Samples Purchased</legend>

        <table class="Table">
            <tr class="Heading">
                <td class="Cell"><span>Voucherno </span></td>
                <td class="Cell"><span>Date </span></td>
                <td class="Cell"><span>Progress </span></td>
                <td class="Cell"><span>Salesman </span></td>
                <td class="Cell"><span>Sample Name </span></td>
                <td class="Cell"><span>Qty </span></td>
                <td class="Cell"><span>Price </span></td>
                <td class="Cell"><span>Disc </span></td>
                <td class="Cell"><span>Total </span></td>
                <td class="Cell"><span>Remarks </span></td>
                <td class="Cell"><span>Status </span></td>
            </tr>

            @foreach (var i in Model.Transaction.PurchasedItems.Where(t => t.ProgressId == 2))
            {
            <tr>
                <td class="Cell">
                    @Html.DisplayFor(m => m.Transaction.voucherno)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => m.Transaction.Date_)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => m.Transaction.Progress)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => m.Transaction.Salesman)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => i.Product)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => i.Qty)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => i.Price)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => i.Disc)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => i.Total)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => i.ProductRemarks)
                </td>
                <td class="Cell">
                    @Html.DisplayFor(m => m.Transaction.Status)
                </td>
            </tr>
            }
        </table>
    </fieldset>