dts*_*tsg 3 c# asp.net-mvc routes actionresult
我正在尝试通过id默认路由中的参数从我的数据库中检索数据:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
在这个ActionResult中,我试图基于route id参数呈现自定义用户控件,以便检索所请求页面的相关数据
public ActionResult InitPageNav(int id)
{
PageModel page = PageNavHelper.GetPageByID(id);
return PartialView("UserControls/_PageNavPartial", page);
}
Run Code Online (Sandbox Code Playgroud)
编辑*
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());
}
Run Code Online (Sandbox Code Playgroud)
但我一直收到错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type
谁能发现我哪里出错?
要调用该操作,URL中需要一个整数,如下所示:/ Home/InitPageNav/1
或者你改变动作方法以允许可以为空的整数(但这没有意义,除非你有一个默认页面,如果没有给出id,你可以检索它).
如果您不想在网址中使用网页ID,则需要其他内容来标识网页,例如标题?
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{title}", // URL with parameters
new { controller = "Home", action = "Index", title = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
和行动:
public ActionResult InitPageNav(String title)
{
PageModel page = PageNavHelper.GetPageByTitle(title);
return PartialView("UserControls/_PageNavPartial", page);
}
Run Code Online (Sandbox Code Playgroud)
只需确保处理title参数为空/ null的情况.通常,您应该使用Mvc框架中已存在的帮助器/扩展来构建您的URL.
@Html.ActionLink("Link text", "action", "controller", new { title = "whatever" }, null)
Run Code Online (Sandbox Code Playgroud)
或者在你的高级助手中,
public static MvcHtmlString CreateMenuItems(this UrlHelper url, string action, string text)
{
var menuItem = new TagBuilder("li");
var link = new TagBuilder("a");
//Get current action from route data
var currentAction = (string)helper.RequestContext.RouteData.Values["action"];
link.Attributes.Add("href", url.Action(action, "home", new { title = "whatever" }));
if (currentAction == action)
{
menuItem.AddCssClass("selected");
}
link.SetInnerText(text);
menuItem.InnerHtml = link.ToString();
return MvcHtmlString.Create(menuItem.ToString());
}
Run Code Online (Sandbox Code Playgroud)
如果ASP .NET MVC框架抛出异常/错误,则此异常的原因是没有在HTTP请求上传递'id'参数.
尝试将操作方法签名重新定义为以下内容:
public ActionResult InitPageNav(int? id) //id is now a nullable int type
{
if(!id.HasValue) //id parameter is set ?
{
//return some default partial view or something
}
PageModel page = PageNavHelper.GetPageByID(id);
return PartialView("UserControls/_PageNavPartial", page);
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果您认为将id参数类型更改为'string'更有用,那么您只需更改操作方法签名即可.
| 归档时间: |
|
| 查看次数: |
27030 次 |
| 最近记录: |