I am attempting to expand on the examples from MattHidinger.com to add pop up dialogs to an MVC site.
Using the code from his site...
<script type="text/javascript">
$.ajaxSetup({ cache: false });
$(document).ready(function () {
$(".openDialog").live("click", function (e) {
e.preventDefault();
$("<div></div>")
.addClass("dialog")
.attr("id", $(this).attr("data-dialog-id"))
.appendTo("body")
.dialog({
title: $(this).attr("data-dialog-title"),
close: function () { $(this).remove() },
modal: true,
width: 700,
height: 350
})
.load(this.href);
});
$(".close").live("click", function (e) {
e.preventDefault();
$(this).closest(".dialog").dialog("close");
});
});
</script>
and an Html.ActionLink
@Html.ActionLink("Delete via actionlink", "Delete", "CostModel"
, new { id = item.HighlightReportID }
, new { @class = "openDialog", data_dialog_id = "dialog", data_dialog_title =
string.Format("Delete {0}", item.DisplayIdentifier) })
I can render an anchor tag that, when clicked, opens a UI Dialog.
<a class="openDialog" data-dialog-id="dialog" data-dialog-title="Delete report for Nov 12" href="/CostModel/Delete/162">Delete via actionlink</a>
But!
If I add an anchor tag manually to the page using the same identifying structures, it fails.
<a class="openDialog" data_dialog_id="dialog" data_dialog_title="Delete report for Nov 12" href="/CostModel/Delete/162">Delete via manual anchor tag</a>
Is there a reason why MVC generated Anchor tags would work, versus manually created tags.
Caveat. Obviously I would not be manually adding tags to a page in MVC. I started by trying to use an ImageLink helper that generates an image inside the anchor tag and it failed too, so going back to first principals I tried the manual approach and was very confused when it failed!
Edit 1
Here is the code from the page, note I have tried setting the data_dialog_id to be the same and different, adding both at the same time and individually, it makes no difference, only the ActionLink will be picked up by the JQuery.
@Html.ActionLink("Delete via actionlink", "Delete", "CostModel"
, new { id = item.HighlightReportID }
, new { @class = "openDialog", data_dialog_id = "dialog1", data_dialog_title = string.Format("Delete {0}", item.DisplayIdentifier) })
<a class="openDialog"
data_dialog_id="dialog2"
data_dialog_title="@string.Format("Delete {0}", item.DisplayIdentifier)"
href="/CostModel/Delete/@item.HighlightReportID">Delete via manual anchor tag</a>
and these are the two tags as rendered
<a class="openDialog" data-dialog-id="dialog1" data-dialog-title="Delete report for Dec 12" href="/CostModel/Delete/212">Delete via actionlink</a>
<a class="openDialog" data_dialog_id="dialog2" data_dialog_title="Delete report for Dec 12" href="/CostModel/Delete/212">Delete via manual anchor tag</a>
As far as my eyes can see they are identical, and yet only the first one (dialog1) will be picked up by the jquery.
onand delegate the click event to the document..onis indeed the recommended way to subscribe to events and.liveis deprecated this won't change anything to the problem here and doesn't explain why the code works when using a helper.<script>tag in the view right after the manual tags are coded. Then you could ensure they are in the right context. Theonevent delegated to the document would catch it where thelivemay not.