MVC 3例外:参数词典共包含参数非可空类型'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

查看:317
本文介绍了MVC 3例外:参数词典共包含参数非可空类型'System.Int32'的'身份证'的方法'System.Web.Mvc无效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 参数词典共包含参数非可空类型'System.Int32'的方法'System.Web.Mvc.ActionResult局(Int32)已'的'ID'无效项COPSGMIS.Controllers.QuestionController。一个可选参数必须是引用类型,可空类型,或声明为可选参数。

不知道怎么回事了?

我的控制器:

 公众的ActionResult局(INT ID)
        {
                QuestionDAL QD =新QuestionDAL();
                变种机构= qd.GetAgencyDetails(ID);
                agency.Reviews = qd.GetAgencyReviews(ID);                返回查看(代理);
        }

我的路线:

 公共静态无效的RegisterRoutes(RouteCollection路线)
        {
            routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);            routes.MapRoute(
                默认,//路线名称
                {控制器} / {行动} / {ID},// URL带参数
                新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
            );        }


解决方案

如果你尝试调用该控制器操作,这个错误被抛出,你不指定 ID 要么在路径部分或作为查询字符串参数。由于您的控制器动作需要一个id作为参数,你应该确保您始终指定此参数。

确认,当你要求这个动作已指定一个有效的 ID 中的网址:

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

如果您要生成锚,确保有一个id:

  @ Html.ActionLink(点击我,代理,新{ID =123})

如果您要发送一个AJAX请求,还请确保id的URL present。

如果在另一方面的参数是可选的,你可以把一个空整型:

 公众的ActionResult局(INT?ID)

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

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.

Not sure whats going wrong?

My controller:

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

                return View(agency);
        }

My routes:

 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
            );

        }

解决方案

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.

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

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

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

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

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例外:参数词典共包含参数非可空类型'System.Int32'的'身份证'的方法'System.Web.Mvc无效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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