MVC 3例外:参数字典包含参数' id'的空条目.不可为null的' System.Int32'用于方法' System.Web.Mvc [英] MVC 3 exception: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc

查看:113
本文介绍了MVC 3例外:参数字典包含参数' id'的空条目.不可为null的' System.Int32'用于方法' System.Web.Mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序似乎运行良好,但是我在log4net日志中不断收到这些异常:

My application seems to run fine, but I keep getting these exceptions in log4net logs:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Agency(Int32)' in 'COPSGMIS.Controllers.QuestionController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

不确定出什么问题了吗

我的控制器:

 public ActionResult Agency(int id)
        {
                QuestionDAL qd = new QuestionDAL();
                var agency = qd.GetAgencyDetails(id);
                agency.Reviews = qd.GetAgencyReviews(id);

                return View(agency);
        }

我的路线:

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

推荐答案

如果尝试调用此控制器操作并且未在路径部分或作为查询字符串参数指定id,则会引发此错误.由于您的控制器操作将id作为参数,因此应确保始终指定此参数.

This error is thrown if you attempt to call this controller action and you do not specify the id either in the path portion or as query string parameter. Since your controller action takes an id as parameter you should make sure that you always specify this parameter.

确保在请求此操作时,已在URL中指定了有效的id:

Make sure that when you are requesting this action you have specified a valid id in the url:

http://example.com/somecontroller/agency/123

如果要生成锚点,请确保有一个ID:

If you are generating an anchor, make sure there's an id:

@Html.ActionLink("click me", "agency", new { id = "123" })

如果您要发送AJAX请求,请确保URL中存在ID.

If you are sending an AJAX request, also make sure that the id is present in the url.

如果另一方面该参数是可选的,则可以将其设为可为空的整数:

If on the other hand the parameter is optional, you could make it a nullable integer:

public ActionResult Agency(int? id)

但是在这种情况下,您将不得不处理未指定参数值的情况.

but in this case you will have to handle the case where the parameter value is not specified.

这篇关于MVC 3例外:参数字典包含参数' id'的空条目.不可为null的' System.Int32'用于方法' System.Web.Mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆