简体   繁体   English

如何从Controller调用ArrayList以在MVC4中查看?

[英]How to call ArrayList from Controller to view in MVC4?

i want to call Arraylist in view. 我想在视图中调用Arraylist。 I will explain my issue clearly. 我会清楚地解释我的问题。

My Controller Code 我的控制器代码

   public ActionResult customerid()
    {
       List<Customer> n = (from c in db.Customers where c.IsDeleted == false  select c).ToList();
        var customertype = string.Empty;

     for (var i = 0; i < n.Count; i++)
        {
            var objCustomerName = n[i].DisplayName;
            var objCustomerID = n[i].CustomerID;
            var objCusCreatedDate=n[i].CreatedDate;
    var objNextDate = objCusCreatedDate.GetValueOrDefault().AddDays(120);
    var salescount = (from sc in db.SalesOrders where sc.CustomerID==objCustomerID && sc.CreatedDate >= objCusCreatedDate && sc.CreatedDate<= objNextDate select sc.SalesOrderID).Count();

            if (salescount <= 3&& salescount> 0)
            {
                customertype = "Exisiting  Customer";
            }
            else if (salescount >= 3)
            {
                customertype = "Potential Customer";
            }
            else
            {
                customertype = "New Customer";
            }

            ArrayList obj = new ArrayList();
            {
                obj.Add(new string[] { objCustomerName, customertype, salescount.ToString()});

            }

             var details = obj;
        }
        return View();
    }

My View Model 我的视图模型

 public class CustomerTypeViewModel
{

    public System.Guid CustomerID { get; set; }
    public string CustomerName { get; set; }
    public DateTime CreatedDate { get; set; }
    public string SalesCount { get; set; }
    public string CustomerType { get; set; }

}

I want to call this array list in view. 我想在视图中调用此数组列表。 How I do that? 我该怎么做? That is i am generating one view based on controller code I need output same as like which is mentioned in the below image . 那就是我基于控制器代码生成一个视图,我需要输出与下图中提到的一样。

Wanted Output 想要的输出

Wanted Output 想要的输出

So i put all the fields (which i going to give as a column in View) in Array list. 所以我将所有字段(我将在“视图”中作为列提供)放在“数组”列表中。 Now i want to call that Arraylist 现在我想叫那个Arraylist

     obj.Add(new string[] { objCustomerName, customertype, salescount.ToString()});

in view . 在视野中。 How I do that? 我该怎么做? I tried to explain my issue as per my level best. 我试图尽我所能来解释我的问题。 please understand my problem and tell me one solution. 请了解我的问题,并告诉我一个解决方案。 I am new to MVC so please help me to solve this problem. 我是MVC的新手,所以请帮助我解决此问题。

In your controller: 在您的控制器中:

List<CustomerTypeViewModel> obj = new List<CustomerTypeViewModel>();
obj.Add(new CustomerTypeViewModel(){
  CustomerName = objCustomerName,
  CustomerType = customertype,
  SalesCount = salescount.ToString()
});

return View(obj);

In your view 在您看来

@model IEnumerable<CustomerTypeViewModel>

and display values like this

@if (Model.Any()) {
  foreach (var m in Model) {
     @Html.DisplayFor(x => m.CustomerName)
     @Html.DisplayFor(x => m.CustomerType)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM