我是MVC的新手
我要显示2和3记录吗?
现在仅显示我要显示2和3记录的第一条记录?
student.cs
public class Employee { public int Id { get; set; } public string Name { get; set; } public string Designaton { get; set; } }
HomeController.cs
public ActionResult Index() { List<Employee> emplist = new List<Employee>() { new Employee(){ Id=1, Name="Steve", Designaton = "hr" }, new Employee(){ Id=2, Name="Bill", Designaton = "finance" }, new Employee(){ Id=3, Name="Ram", Designaton = "hr" }, new Employee(){ Id=4, Name="Ron", Designaton = "finance" }, new Employee(){ Id=5, Name="Rob", Designaton = "finance" } }; ViewBag.totalemp = emplist.Count(); return View(); }
Index.cshtml
@{ ViewBag.Title = "Index"; } <h2>Index</h2> <td>@((ViewBag.totalemp as IEnumerable<migrationdemo.Models.Employee>))</td> @foreach (var item in ViewBag.totalemp) { @item.Id @item.Name @item.Designaton }
我正面临以下错误
哪个地方需要更改
https://i.stack.imgur.com/FQVIp.png
现在只显示一条记录,我也要显示2和3,4,5条记录?
我是mvc新手,我想显示2和3记录?现在只显示第一条记录,我想显示2条和3条记录? student.cs公共班级员工{public int ID {...
您可以在List<Employee>
方法中创建Index
并将其设置为ViewBag
,然后在View中进行迭代。
没有冒犯,但是在启动代码时,您需要了解基本知识。