简体   繁体   English

参数字典包含不可为空类型“System.Int32”的参数“id”的 null 条目。

[英]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.

相关问题 参数字典包含非空类型&#39;System.Int32&#39;的参数&#39;id&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' 参数字典包含非空类型&#39;System.Int32&#39;的参数&#39;_id&#39;的空条目 - The parameters dictionary contains a null entry for parameter '_id' of non-nullable type 'System.Int32' 参数字典包含一个 null 条目,用于不可为空类型“System.Int32”的参数“id” - The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' Create(Int32) 参数字典包含不可为空类型“System.Int32”的参数“id”的空条目 - Create(Int32) the parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' 参数字典包含非空类型为&#39;System.Int32&#39;的参数的空条目 - The parameters dictionary contains a null entry for parameter of non-nullable type 'System.Int32' 参数字典包含非空类型&#39;System.Int32&#39;的参数&#39;SolutionArchitectID&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'SolutionArchitectID' of non-nullable type 'System.Int32' 参数字典包含非空类型“ System.Int32”的参数“ imageWidth”的空条目 - The parameters dictionary contains a null entry for parameter 'imageWidth' of non-nullable type 'System.Int32' 参数字典包含用于方法的非空类型&#39;System.Int32&#39;的参数&#39;userId&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method 参数字典包含非可空类型&#39;System.Int32&#39;的参数&#39;testId&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'testId' of non-nullable type 'System.Int32' 参数字典包含用于方法的非空类型&#39;System.Int32&#39;的参数&#39;foo&#39;的空条目 - The parameters dictionary contains a null entry for parameter 'foo' of non-nullable type 'System.Int32' for method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM