arame3333
arame3333

Reputation: 10223

Cannot get an element in an EditorTemplate

The following EditorTemplate does not work how I would like;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.Models.BusinessUnitSelected>" %>

<tr>
    <td><%: Model.BusinessUnit.BusinessUnitName %></td>
    <td><%: Html.CheckBoxFor(model => model.Selected, 
        new { onclick = "SaveSelection(" + Model.EmployeeId + ", " + Model.BusinessUnit.BusinessUnitId + ", " + Convert.ToInt32(Model.Selected) + ", " + this.ClientID + ")" }) %>
    </td>
</tr>

I want to get the Id of the Checkbox, and this.ClientID fails to do that. This EditorTemplate forms a grid of rows within a table. When a person clicks on the checkbox, the SaveSelection javascript is performed;

    function SaveSelection(employeeId, businessUnitId, selectedFlag, elementId) {
        //var tempFlag = selectedFlag === "0";

        var element = document.getElementById(elementId);
        if (selectedFlag === null) {
            selectedFlag = true;
        } else {
            selectedFlag = !selectedFlag;
        }

        var url = '<%: Url.Action("AddBusinessUnitForEmployee", "DataService")%>';
        dataService.saveSelection(employeeId, businessUnitId, selectedFlag, elementId, SavedSetting, url);
    }

    SavedSetting = function(data) {
        $('#' + data.ElementId).after('<span class="error">' + data.Message + '</span>');
    };

What I want is to display a message next to the checkbox after the server call. So how do I do this? Upticks will be awarded for advice on how I can improve this code.

Upvotes: 0

Views: 83

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039468

You could use HTML5 data-* attributes:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<SHP.Models.BusinessUnitSelected>" 
%>

<tr>
    <td><%: Model.BusinessUnit.BusinessUnitName %></td>
    <td>
        <%= Html.CheckBoxFor(
            x => x.Selected,
            new { 
                data_url = Url.Action("AddBusinessUnitForEmployee", "DataService"),
                data_employeeId = Model.EmployeeId,
                data_businessUnitId = Model.BusinessUnit.BusinessUnitId
            }
       ) %>
    </td>
</tr>

and then in a separate javascript file unobtrusively subscribe to the .click() event of those checkboxes and then fetch the required information from the data-* attributes:

$(function() {
    $('tr input[type="checkbox"]').click(function() {
        var elementId = $(this).attr('id');
        var url = $(this).data('url');
        var employeeId = $(this).data('employeeId');
        var businessUnitId = $(this).data('businessUnitId');
        var selectedFlag = !$(this).is(':checked');

        dataService.saveSelection(
            employeeId, 
            businessUnitId, 
            selectedFlag, 
            elementId, 
            SavedSetting, 
            url
        );
    });
});

Remark: I can't exactly remember if ASP.NET MVC 2 supported the data_ syntax in order to rewrite it to data- syntax in the generated markup. This is defintely supported in ASP.NET MVC 3 and later. If it doesn't work for you, you could use a different overload taking a RouteValueDictionary:

<%= Html.CheckBoxFor(
    x => x.Selected,
    new RouteValueDictionary
    {
        { "data-url", Url.Action("AddBusinessUnitForEmployee", "DataService") },
        { "data-employeeId", Model.EmployeeId },
        { "data-businessUnitId", Model.BusinessUnit.BusinessUnitId }
    }
) %>

Upvotes: 2

Related Questions