请这是我在这里的第一篇文章,我是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>
答案 0 :(得分:1)
当您插入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);
}
}
答案 1 :(得分:0)
问题是您要将同一个对象添加到列表中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);
}