你已经默认索引方法。你需要重载。
您的基本方法:
public ActionResult Index()
{
var studentList = new List<Student>
{
new Student() { StudentId = 1, Studentname = "aa", Age = 18 },
new Student() { StudentId = 2, Studentname = "bbb", Age = 21 }
};
return View(studentList);
}
而且其超载
public ActionResult Index(int id)
{
var studentList = new List<Student>
{
new Student() { StudentId = 1, Studentname = "aa", Age = 18 },
new Student() { StudentId = 2, Studentname = "bbb", Age = 21 }
};
return View(studentList.Where(filter => filter.StudentId == id));
}
你的类的最终情况会是这样:
public class StudentController : Controller
{
public ActionResult Index()
{
var studentList = new List<Student>{ new Student() { StudentId = 1, Studentname = "aa", Age = 18 } , new Student() { StudentId = 2, Studentname = "bbb", Age = 21 } };
return View(studentList);
}
public ActionResult Index(int id)
{
var studentList = new List<Student>
{
new Student() { StudentId = 1, Studentname = "aa", Age = 18 },
new Student() { StudentId = 2, Studentname = "bbb", Age = 21 }
};
return View(studentList.Where(filter => filter.StudentId == id));
}
}
问题是什么? –
您应该首先阅读教程,或者更优选先阅读有关C#的书籍(因为您说自己是初学者),然后介绍ASP.NET MVC。只是在没有正确理解的情况下跳入随机主题,在编程世界中毫无意义。 –
我需要访问指定的列表示例,如果我给... ..../1在url中,我只需要只显示studentid1 –