我是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()); } }更多推荐
发布评论