【问题标题】:Parameter optional from controller来自控制器的可选参数
【发布时间】:2014-05-26 04:24:01
【问题描述】:

我有一个带有 3 个参数的 ActionResult,但是在 y 从另一个 ActionResult 重定向的那一刻,由于 3 个参数为空而抛出错误。

     pubblic ActionResult Comentar()
     {
          return Redirect("GeneralTop");
     }

[HttpGet]
     public ActionResult GeneralTop(string name, int opcion, int id)
     {
             My code
     }

当我从 Comentar 重定向时,视图不起作用。请帮忙? (引擎aspx)

【问题讨论】:

  • 您可以通过使用可空类型(例如int? opcion)来使参数可选
  • 但如果我这样做,在使用 opcion 的变量中抛出下一个错误,不能转换 int?进入int

标签: c# asp.net parameters controller optional


【解决方案1】:
     pubblic ActionResult Comentar()
     {
          return RedirectToAction("GeneralTop",new {name:"test",option:4,id:3});
     }

试试上面的

如果您希望参数是可选的,您应该使用可为空的类型。

【讨论】:

  • 您确定语法正确吗?因为不要让我这样说
  • 工作,但您忘记了新的 {name:"test",option:4,id:3},非常感谢 ;)
猜你喜欢
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 2020-01-30
  • 1970-01-01
  • 2015-03-05
相关资源
最近更新 更多