我是 MVC 新手,并尝试通过 js 导航到 Controller 操作方法。该代码有效(如在操作方法中被击中),但我传入的参数值始终为 null
js:
window.location.href = "@Url.Action("Step1", "Reports")" + "/?" + rsid;
Controller 操作:
public class ReportsController : Controller {
public ActionResult Step1(int? rsid)
{
//do stuff
}
}
rsid 参数始终为空。我尝试过各种链接格式, 例如“/?rsid=[id]”、“/[id]”
我是否需要自定义路由来获取参数?
或者可以用 [httpPost] 或 [httpGet] 注释操作方法?
最佳答案
最简单的方法是将参数命名为“id”,因为它位于默认路由中,并从 js 中调用,如下所示:
'@Html.ActionLink("Step1", "Reports", new { id = ' + rsid + ' })'
public ActionResult Step1(int? id)
{
//do stuff
}
关于c# - 当从 javascript 调用操作时,Asp.net MVC Controller 参数为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29663409/