如何使用HTML-5数据 - *在ASP.NET MVC属性 [英] How to use HTML-5 data-* attributes in ASP.NET MVC

查看:133
本文介绍了如何使用HTML-5数据 - *在ASP.NET MVC属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 HTML5数据 - 在我的ASP.NET MVC 1项目属性。 (我是一个C#和ASP.NET MVC的新手。)

I am trying to use HTML5 data- attributes in my ASP.NET MVC 1 project. (I am a C# and ASP.NET MVC newbie.)

 <%= Html.ActionLink("« Previous", "Search",
     new { keyword = Model.Keyword, page = Model.currPage - 1},
     new { @class = "prev", data-details = "Some Details"   })%>

在上面htmlAttributes数据信息提供以下错误:

The "data-details" in the above htmlAttributes give the following error:

 CS0746: Invalid anonymous type member declarator. Anonymous type members 
  must be declared with a member assignment, simple name or member access.

它的工作原理,当我用data_details,但我猜它需要与数据 - 按规范首发。

It works when I use data_details, but I guess it need to be starting with "data-" as per the spec.

我的问题:


  • 有没有办法得到这个工作,并使用与Html.ActionLink或类似HTML辅助HTML5数据属性?

  • 是否有任何其他的替代机制,以自定义数据附加到一个元素?这个数据将被被JS稍后处理。

推荐答案

更新:MVC 3及更高版本都内置了这种支持。请参阅以下推荐的解决方案JohnnyO高度upvoted答案。

我不认为有实现这一目标的任何直接的帮手,但我有两个想法,您可以尝试:

I do not think there are any immediate helpers for achieving this, but I do have two ideas for you to try:

// 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" ) )%>

只是想法,没有测试过。

Just ideas, haven't tested it.

这篇关于如何使用HTML-5数据 - *在ASP.NET MVC属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆