在ActionLink()或RouteLink()中提供ID?(Providing ID in ActionLink() or RouteLink()?)

编程入门 行业动态 更新时间:2024-10-12 01:28:08
在ActionLink()或RouteLink()中提供ID?(Providing ID in ActionLink() or RouteLink()?)

我是MVC的新手,想要添加~/Destinations/35类的链接,其中它将引用Destinations控制器的Index视图,35是要显示的目标的ID。

ActionLink()或RouteLink()似乎都不允许我创建这样的链接。

另外,我尝试过这样的事情:

<table> @foreach (var d in ViewBag.Results) { <tr> <td> @Html.ActionLink( String.Format("<b>{0}</b>", @Html.Encode(d.Title)), "Details", "Destinations") </td> </tr> } </table>

但是我在ActionLink系列上遇到以下错误,我不明白。

'System.Web.Mvc.HtmlHelper'没有名为'ActionLink'的适用方法,但似乎有一个名称的扩展方法。 无法动态分派扩展方法。 考虑转换动态参数或调用扩展方法而不使用扩展方法语法。

有人可以帮我创建这个链接吗?

I'm new to MVC and would like to add a link to something like ~/Destinations/35, where it would refer to the Index view of the Destinations controller, and 35 is the ID of the destination to be displayed.

Neither ActionLink() or RouteLink() appear to allow me to create a link such as this.

Also, I tried something like this:

<table> @foreach (var d in ViewBag.Results) { <tr> <td> @Html.ActionLink( String.Format("<b>{0}</b>", @Html.Encode(d.Title)), "Details", "Destinations") </td> </tr> } </table>

But I get the following error on the ActionLink line, which I don't understand.

'System.Web.Mvc.HtmlHelper' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

Can someone help me create this link?

最满意答案

您的代码的第一个问题是您尝试在链接文本( <b>标记)中使用HTML,这是不可能的,因为在设计中它始终是HTML编码。

因此,假设您不想在链接中使用HTML,则可以执行以下操作:

@Html.ActionLink(d.Title, "Details", "Destinations", new { id = "35" }, null)

假设你需要在锚内部使用HTML,你有几种可能性:

编写一个自定义的ActionLink帮助程序,它不会对文本进行HTML编码(推荐),然后像这样使用:

@Html.MyBoldedActionLink(d.Title, "Details", "Destinations", new { id = "35" }, null)

一些事情:

<a href="@Url.Action("Details", "Destinations", new { id = "35" })"> <b>@d.Title</b> </a>

因为我推荐第一种方法,这是自定义助手的示例实现:

public static class HtmlExtensions { public static IHtmlString MyBoldedActionLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes ) { var anchor = new TagBuilder("a"); anchor.InnerHtml = string.Format("<b>{0}</b>", htmlHelper.Encode(linkText)); var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); anchor.Attributes["href"] = urlHelper.Action(actionName, controllerName, routeValues); anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return new HtmlString(anchor.ToString()); } }

The first problem with your code is that you are trying to use HTML in the link text (the <b> tags) which is not possible because by design it always HTML encodes.

So assuming you didn't want HTML in the link you could do this:

@Html.ActionLink(d.Title, "Details", "Destinations", new { id = "35" }, null)

And assuming you need HTML inside the anchor you have a couple of possibilities:

Write a custom ActionLink helper which won't HTML encode the text (recommended) and then use like this:

@Html.MyBoldedActionLink(d.Title, "Details", "Destinations", new { id = "35" }, null)

Something along the lines:

<a href="@Url.Action("Details", "Destinations", new { id = "35" })"> <b>@d.Title</b> </a>

and since I recommend the first approach here's a sample implementation of the custom helper:

public static class HtmlExtensions { public static IHtmlString MyBoldedActionLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes ) { var anchor = new TagBuilder("a"); anchor.InnerHtml = string.Format("<b>{0}</b>", htmlHelper.Encode(linkText)); var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); anchor.Attributes["href"] = urlHelper.Action(actionName, controllerName, routeValues); anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return new HtmlString(anchor.ToString()); } }

更多推荐

本文发布于:2023-08-06 23:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RouteLink   ActionLink   Providing   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!