如何使用 - 在名称中指定属性

Rob*_*ker 4 c# asp.net-mvc jquery-mobile

我在Web应用程序中使用ASP.NET MVC和JQueryMobile.我想生成一个链接:

<a href="/Whatever/Previous" data-role="button" data-icon="arrow-l">Previous</a>
Run Code Online (Sandbox Code Playgroud)

我有一个帮助扩展方法,让我做:

<%= Html.ActionLink<WhateverController>(c => c.Previous(), 
         "Previous", 
         new { data-role = "button", data-icon="arrow-l" } ) %>
Run Code Online (Sandbox Code Playgroud)

Ccept 中的属性名除外data-roledata-icon无效.使用@data-role也不起作用.

有什么语法可以解决这个问题吗?或者我仍然坚持创建一个更专业的帮助器,知道正确的属性名称.

svi*_*ick 8

您应该能够使用IDictionary<string, object>而不是匿名对象:

Html.ActionLink<WhateverController>(c => c.Previous(), 
     "Previous", 
     new Dictionary<string, object>
     {
          { "data-role", "button" },
          { "data-icon", "arrow-l"}
     })
Run Code Online (Sandbox Code Playgroud)


Eil*_*lon 8

除了svick的响应之外,我们在ASP.NET MVC 3中进行了一次巧妙的更改,其中包含下划线的属性将自动将下划线转换为破折号.

所以,如果你有这样的代码:

<%= Html.ActionLink<WhateverController>(c => c.Previous(),  
     "Previous",  
     new { data_role = "button", data_icon="arrow-l") %> 
Run Code Online (Sandbox Code Playgroud)

它将使用破折号呈现标记:

<a href="/Whatever/Previous" data-role="button" data-icon="arrow-l">Previous</a>  
Run Code Online (Sandbox Code Playgroud)