【问题标题】:An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters`可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数`
【发布时间】:2017-12-16 22:20:55
【问题描述】:

我正在.net MVC 中创建一个演示应用程序。

下面是来自我的 StudentController 的代码 sn-p。

public ActionResult Edit(int studentId)
{
    var std = studentList.Where(s => s.StudentId == studentId).FirstOrDefault();
    return View(std);
}

[HttpPost]
public ActionResult Edit(Student std)
{
    //write code to update student 

    return RedirectToAction("Index");
}

来自 RouteConfig 的代码 sn-p :

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

当我点击 url http://localhost:54977/student/Edit/1 我得到以下异常。

The parameters dictionary contains a null entry for parameter 'studentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'MVC1.Controllers.StudentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters.

但是当我点击 url http://localhost:54976/student/Edit?StudentId=1 时它工作正常。

我是 .net MVC 的新手。有人可以建议我吗?

【问题讨论】:

  • 请显示您的 RouteMap 设置
  • 我在我的问题中添加了相同的内容..

标签: c# asp.net-mvc


【解决方案1】:

问题是由于您的路由配置造成的。

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

http://localhost:54977/student/Edit/1 中的第三个参数映射到 {id} 而不是 studentId。

您有两种选择来解决这个问题:

1) 更改参数名称

public ActionResult Edit(int id) {
        var std = studentList.Where(s => s.StudentId == id).FirstOrDefault();
        return View(std);
    }

2) 为编辑添加新路由:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
       routes.MapRoute(
            "EditStudent",
            "Edit/{StudentId}",
            new { controller = "Student", action = "Edit" });
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

【讨论】:

    【解决方案2】:

    尝试在您的 RouteConfig 中添加它

    routes.MapRoute(
                    "Student",                                           // Route name
                    "Edit/{StudentId}",                            // URL with parameters
                    new { controller = "Student", action = "Edit" }  // Parameter defaults
                );
    

    【讨论】:

      【解决方案3】:

      没有配置路由

        $.ajax({
                      type: "POST",
                      cache: false,
                      url: '/Student/Edit?StudentId='1,
                      dataType: 'json',
                      ...
                      ...
                      ..
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        相关资源
        最近更新 更多