2009-12-09 41 views
2

我使用的是MVC框架中的Ajax.Actionlink,并且对于我的目标和部分视图,一切正常。但是我已经看到,html输出不会将“id”属性添加到结果<a href=""></a>标记中。带有id属性的Ajax.Actionlink输出

有没有什么办法可以添加它?

回答

3

您需要在对象htmlAttributes参数中使用new { id = "myId" },该参数在其几个构造函数中可用。或者,您可以使用IDictionary<string, string> htmlAttributes在其几个构造函数中填充它。

+0

他需要routeValues参数。 HtmlAttributes会设置锚标签本身的id属性。 – 2009-12-09 22:37:24

+0

我对他的问题的理解是,他想要a 2009-12-09 22:40:09

+0

我认为这就是要求 - “将”id“属性添加到结果标签”?这个答案听起来正确。 – 2009-12-09 22:42:27

-2

我相信你正在寻找routeValues参数。

<%= Ajax.ActionLink("SomeAction", "SomeController", new { id = ID_HERE }, null) %> 
+0

-1:这是不正确的。这会向路由添加一个参数,而不是锚点标记(OP要求的)的id属性。 – 2014-01-23 17:15:17

7

尝试重载之一,像this one

public static string ActionLink(
    this AjaxHelper ajaxHelper, 
    string linkText, 
    string actionName, 
    Object routeValues, 
    AjaxOptions ajaxOptions, 
    Object htmlAttributes 
) 

然后你就可以在htmlAttributes指定ID参数,例如

new { id = "myIdValue" } 
+0

+1:这也是正确的,但5分钟后...很好的例子。 – 2014-01-23 17:17:18