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.
You could use HTML5
data-*attributes:and then in a separate javascript file unobtrusively subscribe to the
.click()event of those checkboxes and then fetch the required information from thedata-*attributes:Remark: I can’t exactly remember if ASP.NET MVC 2 supported the
data_syntax in order to rewrite it todata-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 aRouteValueDictionary: