Estoy haciendo una lista de estudiantes para mostrarla en una vista Razor mediante ViewData.
controlador
using ViewData.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ViewBag.Controllers
{
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
IList<Student> studentList = new List<Student>();
studentList.Add(new Student() { StudentName = "Bill" });
studentList.Add(new Student() { StudentName = "Steve" });
studentList.Add(new Student() { StudentName = "Ram" });
ViewData["students"] = studentList;
return View();
}
}
}
Clase estudiante
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ViewData.Models
{
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
vista
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>students</title>
</head>
<body>
<div>
<ul>
@foreach (var std in ViewData["students"] as IList<Student>)
{
<li>
@std.StudentName
</li>
}
</ul>
</div>
</body>
</html>
en la vista quiero recorrer la vista pero me indica que en @foreach (var std in ViewData["students"] as IList<Student>)
Student no me lo está reconociendo. Que puede ser?