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

时间:2016-03-28 05:27:20

标签: c# asp.net-mvc-4 arraylist

我想在视野中打电话给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; }

}

我想在视图中调用此数组列表。我怎么做的?那就是我基于控制器代码生成一个视图,我需要输出相同,如下图所示。

通缉输出

Wanted Output

所以我将所有字段(我将在View中作为列提供)放在数组列表中。现在我想称之为Arraylist

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

在视野中。我怎么做的?我试着根据我的水平最好地解释我的问题。请理解我的问题并告诉我一个解决方案。我是MVC的新手,所以请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

在您的控制器中:

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)
  }
}
相关问题