简体   繁体   English

计算asp.net mvc中视图中的项目数

[英]count the number of items in view in asp.net mvc

I am very new to asp.net development. 我对asp.net开发非常陌生。 In my asp.net mvc project I have model "Employee" and I'm passing a list of "Employee" model to a RAZOR view and I'm trying to count different type of employees and show a summary. 在我的asp.net mvc项目中,我具有模型“ Employee”,并且将“ Employee”模型的列表传递给RAZOR视图,并且我试图计算不同类型的雇员并显示摘要。

my view is like this, 我的看法是这样的

@{
    int available = 0;
    int onLeave = 0;
    int away = 0;
    int unAvailable = 0;
}

@foreach (var employee in Model){
  <lable>@employee.Name</lable></br>
   @if (@employee.Available){
      @available=available+1;
  }
   @if (@employee.Unavailable){
      @unAvailable=unAvailable;
  }
   @if (@employee.Away){
      @away=away+1;
  }
   @if (@employee.Onleave){
       @onLeave=onLeave+1;
  }

}
    <div>
        <!--additional summary is displayed here-->
        <label>Available:</label>@available
        <label>Unavailable:</label>@unAvailable
        <label>Away:</label>@away
        <label>On Leave:</label>@onLeave
    </div>

but when I run the my project variables "available","unAvailable","away" and "onLeave" don't get updated. 但是当我运行项目变量“ available”,“ unAvailable”,“ away”和“ onLeave”时,不会更新。

I'm sure that list is not empty because employee names are displaying. 我确定该列表不是空的,因为正在显示员工姓名。 can some explain me what is happening here and correct way of doing this 可以给我解释一下这里发生了什么以及正确的做法

You should be doing this outside the before passing to the view like I mentioned in my original comment. 在传递到我在原始评论中提到的视图之前,您应该在外部进行此操作。 You can create a new object called a ViewModel to represent the data exactly like you want it on the page. 您可以创建一个称为ViewModel的新对象,以完全按照页面上的样子表示数据。 So I created a simple example, I only used the 4 properties of Employee you are displaying in you CSHTML page. 因此,我创建了一个简单的示例,我仅使用了CSHTML页面中显示的Employee的4个属性。 On your View where you said your MODEL is either a list, arrary or whatever of Employee change it to EmployeeViewModel. 在您说过的View上,您的MODEL是列表,清单或任何Employee,都可以将其更改为EmployeeViewModel。 Then in your controller where you get your list of employees set them to the Employees property of the Employee ViewModel. 然后,在获取您的雇员列表的控制器中,将其设置为Employee ViewModel的Employees属性。

public class EmployeeViewModel
{
    public IEnumerable<Employee> Employees { get; set; }
    public int TotalAvailable { get { return Employees.Count(emp => emp.Available); } }
    public int TotalUnavailable { get { return Employees.Count(emp => emp.Unavilable); } }
    public int TotalAway { get { return Employees.Count(emp => emp.Away); } }
    public int TotalOnLeave { get { return Employees.Count(emp => emp.OnLeave); } }

}

public class Employee
{
    public bool Available { get; set; }
    public bool Unavilable { get; set; }
    public bool Away { get; set; }
    public bool OnLeave { get; set; }
}

//In the controller do this.
public ActionResult Index() //use your controller Action Name here
{
    var employeeViewModel = new EmployeeViewModel { Employees = /*Your list of empoyees you had as a Model before here*/}
    return View(employeeViewModel)
}

Change your CSHTML code to something like this: 将您的CSHTML代码更改为以下内容:

@foreach(var employee in Model.Employees)
{
  <label> @employee.Name </label></br>
}
    <div>
        <!--additional summary is displayed here-->
        <label> Available:</label> @Model.TotalAvailable
        <label> Unavailable:</label> @Model.TotalUnavailable
        <label> Away:</label> @Model.TotalAway
        <label> On Leave:</label> @Model.TotalOnLeave
    </div>

Mvc sample on how to do it: Mvc示例如何做:

you need a model class 你需要一个模型课

public class EmployeeModel
{
        public int Available {get; set;}
        public int OnLeave  {get; set;}
        public int Away {get; set;}
        public int UnAvailable  {get; set;}
}

and a command: 和一个命令:

  public ActionResult Index()
  {
    var model = new EmployeeModel();
    model.Available = employee.count(e=> e.available);
    model.OnLeave = employee.count(e=> e.onLeave);
    model.Away = employee.count(e=> e.away);
    model.UnAvailable = employee.count(e=> e.unAvailable );
    return View(model);
  }

and a view 和一个视图

@model EmployeeModel
   <div>
        <!--additional summary is displayed here-->
        <label>Available:</label>@Model.Available
        <label>Unavailable:</label>@Model.UnAvailable
        <label>Away:</label>@Model.Away
        <label>On Leave:</label>@Model.OnLeave
   </div>

An easy and quick way is: 一种简单快捷的方法是:

<div>
    <!--additional summary is displayed here-->
    <label>Available:</label>@Model.Count(i => i.Available)<br>
    <label>Unavailable:</label>do the same.
    <label>Away:</label>do the same.
    <label>On Leave:</label>do the same.
</div>

Make sure the model has already been "ToList()", or it might lead to mult-access of database. 确保模型已经是“ ToList()”,否则可能导致对数据库的多重访问。

Basically, I only use viewmodel when I need to pass more than 1 models to the view. 基本上,仅在需要将多个模型传递给视图时才使用viewmodel。 Not worth in this case. 在这种情况下不值得。

Make such calculations in View considered a BAD practice . 在View中进行这样的计算被认为是BAD做法

In your case better option will be create ViewModel with corresponding properties and then pass it to the model, previously calculating count for every type in controller using LINQ. 在您的情况下,更好的选择是创建具有相应属性的ViewModel ,然后将其传递给模型,事先使用LINQ计算控制器中每种类型的计数。 Where you could reference your types like Model.available, Model.away and so on. 您可以在其中引用Model.available, Model.away等类型的Model.available, Model.away Using ViewModel it is the best practice for MVC. 使用ViewModel是MVC的最佳实践。

@Thorarins answer show you how to use LINQ in your code to calculate count for you types. @Thorarins答案向您展示了如何在代码中使用LINQ来计算类型的计数。

UPDATE: You can use JS, but you should not, because it still not what supposed to happen in View. 更新:您可以使用JS,但不能使用JS,因为它仍然不是View中应该发生的事情。 Work with data should not be handled in View. 不应在View中处理数据。 Don't be scared by ViewModels, they not that hard as it could seem. 不要被ViewModel吓到,它们看起来并不那么难。 Please read this article which consider all ways to pass data to View, which has good example how create and pass ViewModel. 请阅读本文 ,其中考虑了将数据传递给View的所有方法,其中有一个很好的示例说明如何创建和传递ViewModel。

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

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