简体   繁体   English

MVC路由参数为空

[英]MVC Route Parameters Are Null

I'm receiving the following error that my default route parameters are null. 我收到以下错误,我的默认路由参数为空。 I've used this same code on a Controller Action that didn't have any parameters in the URL and it worked fine. 我在Controller Action上使用了相同的代码,但在URL中没有任何参数,并且效果很好。 I know that my custom route is being called but I don't understand why startIndex and pageSize are showing up null in the action. 我知道正在调用我的自定义路由,但我不明白为什么startIndex和pageSize在操作中显示为null。

Error: 错误:

The parameters dictionary contains a null entry for parameter 'startIndex' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewVcByStatus(System.String, Int32, Int32)' in 'AEO.WorkOrder.WebUI.Controllers.VendorComplianceController'. 参数字典在'AEO.WorkOrder.WebUI'中包含方法'System.Web.Mvc.ActionResult ViewVcByStatus(System.String,Int32,Int32)'的非空类型'System.Int32'的参数'startIndex'的空条目。 .Controllers.VendorComplianceController”。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。

Parameter name: parameters 参数名称:参数

Controller: 控制器:

public ActionResult ViewVcByStatus(string status, int startIndex, int pageSize) { ... }

Route: 路线:

routes.MapRoute("ViewVcByStatus", "ViewVcByStatus/{status}",
  new
  {
    controller = "VendorCompliance",
    action = "ViewVcByStatus",
    startIndex = 0,
    pageSize = WebConfigurationManager.AppSettings["PageSize"],
   });

Link: 链接:

<a href="VendorCompliance/ViewVcByStatus?status=PROCESSED">

Also tried this link which produces the same error: 还尝试了产生相同错误的此链接:

<a href="VendorCompliance/ViewVcByStatus/PROCESSED">

Try this. 尝试这个。

public ActionResult ViewVcByStatus(string status, int? pageSize, int?startIndex)
    {
        return View();
    }

Route.config Route.config

routes.MapRoute(
            name: "ViewVcByStatus",
            url: "ViewVcByStatus/{status}",
            defaults: new { controller = "VendorCompliance", action = "ViewVcByStatus", startIndex = UrlParameter.Optional, pageSize = UrlParameter.Optional });

optional parameters should be declared optional in routeconfig and mark them int? 可选参数应在routeconfig中声明为可选,并将其标记为int? in your action method, This will do the work for you. 在您的操作方法中,这将为您完成工作。 Hope this helps.This solution will work with your url pattern in your question " http://localhost:53290/VendorCompliance/ViewVcByStatus?status=PROCESSED ". 希望对您有所帮助。此解决方案将与您在问题“ http:// localhost:53290 / VendorCompliance / ViewVcByStatus?status = PROCESSED ”中使用的网址格式一起使用。

发送带有链接的startIndex和pageSize(我将其硬编码,改用参数),您的actionresult期望该链接需要提供的所有参数,并且MapRoute可能会陷入默认Route,因为它无法与任何路径匹配与您提供的一个参数匹配的其他路线

<a href="VendorCompliance/ViewVcByStatus?status=PROCESSED&startIndex=0&pageSize=0">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM