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.
So, how can I make it wok?
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"
.
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly