Using data attribute with mvc html helpers

We can use data attribute with MVC html helpers by replacing hyphen with underscore. View will convert underscore as hyphen after rendering.

<div class="form-group">
<label for="dlstCourses" class="active"></label>
@Html.DropDownList("Course", @Model.Courses, new { @class = "form-control",@id="dlstCourses", @data_userid= @Model.UserId })

Here, you can access like this.

$(document).ready(function () {

 var UserId = $('#dlstCourses').data('userid');



Labels: data attribute, mvc html helpers, mvc html helpers with data attributes

Receive Quality Tutorials Straight in your Inbox by submitting your Email below:

Delivered by FeedBurner

Protected by Copyscape Duplicate Content Checker