[英]The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' . .
I'm facing a problem that I couldn't solve.我面临一个我无法解决的问题。 I get this message on my browser:
我在浏览器上收到此消息:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Students(Int32)' in 'FirstProject.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
I will give you the codes that I see related to the problem:我会给你我看到的与问题相关的代码:
RouteConfig.cs路由配置.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace FirstProject
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
HomeController.cs家庭控制器.cs
using FirstProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FirstProject.Controllers
{
public class HomeController : Controller
{
level_8_coursesEntities db = new level_8_coursesEntities();
public ActionResult Index()
{
List<level_8_courses> list = db.level_8_courses.ToList();
return View(list);
}
public ActionResult Numberofstudents()
{
List<level_8_courses> list = db.level_8_courses.ToList();
return View(list);
}
public ActionResult Students(int id)
{
List<student> studentlist = db.students.Where(x => x.corId == id).ToList();
return View(studentlist);
}
Students学生
@using FirstProject.Models;
@model List<student>
@foreach (var item in Model)
{
<p>@item.sName</p>
}
I do not have enough experience in programming, so please explain in detail what I do in each file and what I write.我没有足够的编程经验,所以请详细说明我在每个文件中做了什么以及我写了什么。
Thank you very much.非常感谢。
The action method public ActionResult Students(int id)
is declared with parameter id
as int
type, but you hit this method with non-number value.动作方法
public ActionResult Students(int id)
使用参数id
声明为int
类型,但是您使用非数字值点击此方法。
For example, you can change the method signature, like below:例如,您可以更改方法签名,如下所示:
public ActionResult Students(int? id)
{
List<student> studentlist = null;
if (id.HasValue)
{
studentlist = db.students.Where(x => x.corId == id).ToList();
}
else
{
// Set the `studentlist` to some default value when `id` doesn't defined or not a number.
studentlist = ...
}
return View(studentlist);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.