In my MVC project I have this Editor Template.
I cannot debug it in the IE Developer Tools as it is in a popup window. I do not have access to the other browsers.
So my jquery is not picking up the values for the url, employeeId and the businessUnitId.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.Models.BusinessUnitSelected>" %>
<tr>
<td><%: Model.BusinessUnit.BusinessUnitName %></td>
<td>
<%: Html.CheckBoxFor(
x => x.Selected,
new RouteValueDictionary
{
{ "data-url", Url.Action("AddBusinessUnitForEmployee", "DataService") },
{ "data-employeeId", Model.EmployeeId },
{ "data-businessUnitId", Model.BusinessUnit.BusinessUnitId }
}
) %>
</td>
</tr>
<script type="text/javascript">
$(document).ready(function () {
$('tr input[type="checkbox"]').click(function () {
var elementId = $(this).attr('id');
alert("elementId = " + elementId);
var url = $(this).val('data-url');
alert("url = " + url);
var employeeId = $(this).val('data-employeeId');
alert("employeeId = " + employeeId);
var businessUnitId = $(this).val('data-businessUnitId');
alert("businessunitId = " + businessUnitId);
var selectedFlag = $(this).is(':checked');
alert("selectedFlag = " + selectedFlag);
dataService.saveSelection(
employeeId,
businessUnitId,
selectedFlag,
elementId,
SavedSetting,
url
);
});
});
</script>
.val()is used to get/set thevalueattribute,you should use .attr()instead:eg.$(this).attr('data-url');edit
@Dima Suggests the correct way to access HTML5
data-*attributes – by using.data(), eg.$(this).data('url');note: You should hyphenate words in your
data-attributes otherwise you’ll have trouble reading their values when using.data(). See below:-This is because
$('input[type="text"]').data('employeeId')is trying to readdata-employee-id, using the latter you can then access the data using.data('employeeId')or.data('employee-id').