无法理解问题所在......我的溃败值是:
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。有人可以帮忙吗?
答案 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);
}