문제

I would like to use a javascript library that hits the data- attribute in the link i have created.

@model IEnumerable<string>


@foreach (var link in Model)
{
    @Html.RouteLink(
      link,
      new { controller = "Product", action = "List", category = link, page = 1 },
      new
      {
          @class = link == ViewBag.SelectedCategory ? "selected" : null, 
          id= link.Replace(" ","")
      }
    )
}

I am able to add a class and a id but when i try to add data-filter it returns and error

 @class = link == ViewBag.SelectedCategory ? "selected" : null, 
      data-filter = link.Replace(" ","")

Invalid annonymous type

도움이 되었습니까?

해결책 2

@class = link == ViewBag.SelectedCategory ? "selected" : null, 
  data_filter = link.Replace(" ","")

As D.R. has pointed out, MVC has the option of turning underscores in dashes.

다른 팁

You cannot use dashes in property names. The solution is to use underscores instead: data_filter = .... ASP.NET MVC automatically converts the underscore to a dash afterwards.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top