无法在actionLink中传递参数ID

时间:2012-08-03 20:23:56

标签: asp.net-mvc-3

无法理解问题所在......我的溃败值是:

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

我尝试传递参数id:

@Html.ActionLink(app.Name, "SingleAppEdit", "Admin", new { id = app.Id }, null)

我在Controller“Admin”中的操作:

public ActionResult SingleAppEdit(string appId)
        {
            var positions = new List<SelectListItem>
                                       {
                                           new SelectListItem() {Text = "Top", Value = "Top"},
                                           new SelectListItem() {Text = "Bottom", Value = "Bottom"},
                                           new SelectListItem() {Text = "None", Value = "None"}
                                       };
            ViewData["PositionsList"] = new SelectList(positions, "Value", "Text");


            var app = Apps.FirstOrDefault(a => a.Id == Convert.ToInt32(appId));
            return View(app);
        }

我在控制器中得到null。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:6)

您的方法参数名称为appId。但是您尝试传递名为id的参数。

解决方案:更改参数名称以与方法定义匹配。

@Html.ActionLink(app.Name, "SingleAppEdit", "Admin",
                                new { appId= app.Id }, null)

答案 1 :(得分:1)

将控制器中的appID更改为ID,如下所示:

public ActionResult SingleAppEdit(string id)
        {
            var positions = new List<SelectListItem>
                                       {
                                           new SelectListItem() {Text = "Top", Value = "Top"},
                                           new SelectListItem() {Text = "Bottom", Value = "Bottom"},
                                           new SelectListItem() {Text = "None", Value = "None"}
                                       };
            ViewData["PositionsList"] = new SelectList(positions, "Value", "Text");


            var app = Apps.FirstOrDefault(a => a.Id == Convert.ToInt32(id));
            return View(app);
        }