我正在尝试通过id
默认路由中的参数从我的数据库中检索数据:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
在此 ActionResult 中,我尝试根据路由 id 参数呈现自定义用户控件,以便检索所请求页面的相关数据
public ActionResult InitPageNav(int id)
{
PageModel page = PageNavHelper.GetPageByID(id);
return PartialView("UserControls/_PageNavPartial", page);
}
编辑*
public static MvcHtmlString CreateMenuItems(this HtmlHelper helper, string action, string text)
{
var menuItem = new TagBuilder("li");
var link = new TagBuilder("a");
//Get current action from route data
var currentAction = (string)helper.ViewContext.RouteData.Values["action"];
link.Attributes.Add("href", string.Format("/Home/{0}", action));
if (currentAction == action)
{
menuItem.AddCssClass("selected");
link.Attributes.Remove("href");
link.Attributes.Add("href", string.Format("/Home/{0}", currentAction.ToString()));
}
link.SetInnerText(text);
menuItem.InnerHtml = link.ToString();
return MvcHtmlString.Create(menuItem.ToString());
}
但我不断收到错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type
谁能发现我哪里出错了?