在列表中显示重复项的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在列表中显示重复项的问题相关的知识,希望对你有一定的参考价值。
请问这是我在这里的第一篇文章,我是ASP MVC 5的新手。我已经在这个问题上工作了2天,但我不能找到解决方案。我试图将一些值分配给列表,创建一个viewmodel,然后将ViewModel中的值传递给控制器中的View。
问题是它完全显示列表,但它只显示列表中的最后一项(Tirus Peters)六次,而不是显示列表中的其他项目。我不知道我做错了什么。
这些是我的代码:
该模型:
namespace Learning.Models
{
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public double Salary { get; set; }
public Employee(int eID, string first, string Last, double sal)
{
this.EmployeeID = eID;
this.FirstName = first;
this.LastName = Last;
this.Salary = sal;
}
}
}
ViewModel:
namespace Learning.ViewModel
{
public class EmloyeeViewModel
{
public int EmployeeID { get; set; }
public string FullName { get; set; }
public string Salary { get; set; }
public string salcolor { get; set; }
}
}
指数控制器:
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
EmloyeeViewModel em = new EmloyeeViewModel();
List<EmloyeeViewModel> emVM = new List<EmloyeeViewModel>();
List<Employee> emp = new List<Employee>()
{
new Employee(1, "Tony", "Cruise", 231.89),
new Employee(2, "Bill", "George", 152.11),
new Employee(3, "John", "Bill", 7651.29),
new Employee(4, "Donald", "Kay", 1500.08),
new Employee(5, "Smith", "Bill", 28.91),
new Employee(6, "Tirus", "Peter", 1128.91)
};
foreach (Employee f in emp)
{
em.EmployeeID = f.EmployeeID;
em.FullName = f.FirstName + " " + f.LastName;
em.Salary = f.Salary.ToString("C");
if (f.Salary > 100)
{
em.salcolor = "green";
}else
{
em.salcolor = "red";
}
emVM.Add(em);
}
return View(emVM);
}
}
索引视图:
@using Learning.ViewModel
@model IList<EmloyeeViewModel>
<h2>Got It!!</h2>
<table>
<tr><th>Employee ID</th><th>Full Name</th><th>Salary</th></tr>
@foreach (var a in Model)
{
<tr>
<td>@a.EmployeeID</td>
<td>@a.FullName</td>
<td><span style="background-color:@a.salcolor">@a.Salary</span></td>
</tr>
}
</table>
答案
插入EmployeeViewModel
对象时,由于只在foreach
循环外部构造了一个对象,因此一次又一次地插入具有相同引用的相同对象。因此,您的列表将包含所有项目将具有相同的值在foreach
循环中迭代的最后一项,因为它们共享相同的参考foreach
循环实际上在同一对象上应用新值,这基本上是List
中的所有项目
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
List<EmloyeeViewModel> emVM = new List<EmloyeeViewModel>();
List<Employee> emp = new List<Employee>()
{
new Employee(1, "Tony", "Cruise", 231.89),
new Employee(2, "Bill", "George", 152.11),
new Employee(3, "John", "Bill", 7651.29),
new Employee(4, "Donald", "Kay", 1500.08),
new Employee(5, "Smith", "Bill", 28.91),
new Employee(6, "Tirus", "Peter", 1128.91)
};
foreach (Employee f in emp)
{
EmloyeeViewModel em = new EmloyeeViewModel();
em.EmployeeID = f.EmployeeID;
em.FullName = f.FirstName + " " + f.LastName;
em.Salary = f.Salary.ToString("C");
if (f.Salary > 100)
{
em.salcolor = "green";
}
else
{
em.salcolor = "red";
}
emVM.Add(em);
}
return View(emVM);
}
}
另一答案
问题是您将相同的对象添加到列表中6次。您需要在每次迭代时创建一个EmployeeViewModel
的新实例。见下面的代码。
foreach (Employee f in emp)
{
// Create a new instance for each item.
em = new EmployeeViewModel();
em.EmployeeID = f.EmployeeID;
em.FullName = f.FirstName + " " + f.LastName;
em.Salary = f.Salary.ToString("C");
if (f.Salary > 100)
{
em.salcolor = "green";
}
else
{
em.salcolor = "red";
}
emVM.Add(em);
}
以上是关于在列表中显示重复项的问题的主要内容,如果未能解决你的问题,请参考以下文章
如何使用列表视图在选项卡式活动的片段中呈现 SQLite 表数据 [重复]
带有 SwiftUI 的 Apple Watch 上列表项的背景 [重复]