繁体   English   中英

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

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

我想在视图中调用Arraylist。 我会清楚地解释我的问题。

我的控制器代码

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

我的视图模型

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

}

我想在视图中调用此数组列表。 我该怎么做? 那就是我基于控制器代码生成一个视图,我需要输出与下图中提到的一样。

想要的输出

想要的输出

所以我将所有字段(我将在“视图”中作为列提供)放在“数组”列表中。 现在我想叫那个Arraylist

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

在视野中。 我该怎么做? 我试图尽我所能来解释我的问题。 请了解我的问题,并告诉我一个解决方案。 我是MVC的新手,所以请帮助我解决此问题。

在您的控制器中:

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

return View(obj);

在您看来

@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