我正在尝试在ASP.NET MVC 1项目中使用HTML5数据属性。(我是C#和ASP.NET MVC新手。)
<%= Html.ActionLink("« Previous", "Search", new { keyword = Model.Keyword, page = Model.currPage - 1}, new { @class = "prev", data-details = "Some Details" })%>
上面的htmlAttributes中的“数据细节”给出以下错误:
CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
当我使用data_details时,它可以工作,但是我想它必须按照规范以“ data-”开头。
我的问题:
更新:MVC 3和更高版本对此提供了内置支持。 有关建议的解决方案,请参见下面JohnnyO的高度评价的答案。
我认为没有任何直接的帮助者可以实现这一目标,但是我确实有两个想法可供您尝试:
// 1: pass dictionary instead of anonymous object <%= Html.ActionLink( "back", "Search", new { keyword = Model.Keyword, page = Model.currPage - 1}, new Dictionary<string,Object> { {"class","prev"}, {"data-details","yada"} } )%> // 2: pass custom type decorated with descriptor attributes public class CustomArgs { public CustomArgs( string className, string dataDetails ) { ... } [DisplayName("class")] public string Class { get; set; } [DisplayName("data-details")] public string DataDetails { get; set; } } <%= Html.ActionLink( "back", "Search", new { keyword = Model.Keyword, page = Model.currPage - 1}, new CustomArgs( "prev", "yada" ) )%>
只是想法,还没有测试。