How to write HTML5 data-atrribute in Razor MVC C#?


I am trying to specify HTML5 data-atrribute in my asp.net MVC razor code like below

 @Html.DropDownListFor(m => m.Industry, OnePassV3.utility.OptionList.GetIndustryListSelectList()
                           , new { @class="cs-select cs-skin-slide cs-transparent", @data-init-plugin="cs-select" })

but i am getting error as you can see below in the image.

how-to-specify-data-attribute-mvc-razor

So, how can I make it wok?


Asked by:- jaya
1
: 3313 At:- 10/23/2017 11:28:56 AM
MVC C# Razor data-attribute-in-razor HTML5







2 Answers
profileImage Answered by:- Sam

You can show data-attribute in Razor C#, by simply changing dashes(-) into underscrore, so your above code will be like this

@Html.DropDownListFor(m => m.Industry, OnePassV3.utility.OptionList.GetIndustryListSelectList()
                           , new { @class="cs-select cs-skin-slide cs-transparent", @data_init_plugin="cs-select" })

it will work in Razor view and will be rendered as data-init-plugin="cs-select".

2
At:- 10/24/2017 7:29:40 AM
Working now :) 0
By : jaya - at :- 11/5/2017 11:52:48 AM


profileImage Answered by:- vikas_jk

You can try using "_" instead of "-" in MVC 4 or above, razor syntax

@Html.TextBoxFor(
    model => model.IteName, 
    new { data_url = Url.Action("ActionMethod", "Controller") }
)

If you are using older version of MVC

<%= Html.TextBoxFor(
    model => model.ItemName, 
    new Dictionary<string, object> { 
        { "data-url", Url.Action("ActionMethod", "Controller") } 
    }
) %>

That's it.

0
At:- 10/3/2021 3:43:12 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use