Sneha Dhandapani

Sneha Dhandapani

  • 1.5k
  • 471
  • 137.1k

ASP.Net MVC Razor Sum and Count functions?

Mar 29 2016 1:07 AM
Hi i want to count the total number of rows in my view . I will explain my issue clearly..
 
My output
 
 
 
1)  i want to count the total no of rows in the view and  want to display  that count in the same view of right top corner
2)  I have three Customer Type New customer, Potential Customer, Existing Customer. i want to count the each type and want to display it in right top corner of the view.
 
I gave as like this @Model.Sum(i => i.CustomerName)  but it showing error and i donno where i have to declare this line  and how to calculate the Types and show in the view. please any one give 
suggestion or solution for my problem.
 
 My View
 
@model IEnumerable<Sample_Customer.Models.CustomerTypeViewModel>
@{
ViewBag.Title = "customerid";
}
<h2>CustomerType</h2>
<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/jquery-ui.min.js"></script>
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title" style="color: #0000FF">Customer</h3>
<div class="box-body">
<table id="CustomerTable" class="table table-bordered table-striped">
<thead>
<tr>
<th style="color: #1a1ae9">Customer Name</th>
<th style="color:#1a1ae9">Customer Type</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
<tr>
<td>
@i.CustomerName
</td>
<td>
@i.CustomerType
</td>
</tr>
@Model.Sum(i => i.CustomerName) (getting error in this line)
}
</tbody>
<tfoot>
<tr>
<th style="color: #1a1ae9">Customer Name</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
 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; }
}
My controller
 
public ActionResult customerid()
{
List<Customer> n = (from c in db.Customers where c.IsDeleted == false select c).ToList();
var customertype = string.Empty;
List<CustomerTypeViewModel> obj = new List<CustomerTypeViewModel>();
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 = "potential Customer";
}
else if (salescount >= 3)
{
customertype = "Existing Customer";
}
else
{
customertype = "New Customer";
}
obj.Add(new CustomerTypeViewModel()
{
CustomerName = objCustomerName,
CustomerType = customertype,
SalesCount = salescount.ToString()
});
}
return View(obj);
}
 
 
 
 
 
 

Answers (11)