1

无法理解问题出在哪里......我的溃败值是:

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)

对于我在控制器“管理员”中的操作:

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);
        }

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

4

2 回答 2

6

您的方法参数名称是appId. 但是您正在尝试传递一个名为id.

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

@Html.ActionLink(app.Name, "SingleAppEdit", "Admin",
                                new { appId= app.Id }, null)
于 2012-08-03T20:25:31.913 回答
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);
        }
于 2012-08-03T20:25:28.837 回答