参数字典包含非可空类型'System.Int32'的参数'classRoomId'的空条目用于方法

时间:2013-04-27 19:40:52

标签: asp.net-mvc

 I have a action method in studentController which filter students by classroomId

    public ActionResult ClassStudents(int classRoomId)
    {

        var students = st.GetAll().Where(s => s.ClassRoomID == classRoomId);

        var classroom = clr.GetByID(classRoomId);

        ViewBag.ClassRoomTitle = classroom.ClassRoomTitle;

        return View(students);
    }




when i input  /Student/ClassStudents/1

it give me a error as:

参数字典包含参数'classRoomId'的空条目    方法'System.Web.Mvc.ActionResult'的非可空类型'System.Int32'  ClassStudents(的Int32)'

你能找到问题吗? 感谢

2 个答案:

答案 0 :(得分:0)

替换

int classRoomId

使用

int? classRoomId

如果要处理空值或使用

int classRoomId = 0

如果你想要一个默认值而不是null。

答案 1 :(得分:0)

变化:

public ActionResult ClassStudents(int ClassRoomId)

public ActionResult ClassStudents(int? ClassRoomId)

这将使ClassRoomId 可以为空