@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.
Question
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
Solution 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.
OTHER TIPS
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.