Reputation: 3
In my StudenntController I hvae a method
public ActionResult ClassStudents(int? classRoomId)
{
var students = st.GetAll().Where(s => s.ClassRoomID == classRoomId);
ViewBag.ClassRoomTitle = clr.GetAll().Where(c=>c.ClassRoomID == classRoomId).Single().ClassRoomTitle;
return View(students);
}
when i enter localhostxxxx/Student/ClassStudent/1 the parameter classRoomId is null and i got a error: Sequence contains no elements I wonder why url: "{controller}/{action}/{id}" do not pass the parameter?
Upvotes: 0
Views: 940
Reputation: 1717
In order for the route to work, the parameter must be named id
. In your code, the parameter is named classRoomId
. Change your method signature to:
public ActionResult ClassStudents(int? id)
{
// Your code here
}
If you really want to name the parameter classRoomID
then you would need to create a custom route.
Upvotes: 1
Reputation: 15881
you can set id paramter in routing .
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Student", action = "ClassStudents", id = UrlParameter.Optional }
);
parameter should match the route values
public ActionResult ClassStudents(int id)
{
// Your code here
}
Upvotes: 2