In _CreateOrUpdate.cshtml in Administration/Views/Product, I have the following (skipping straight down to my div and script since the grid/buttons, etc. are pretty long):
...
<div id="divComment">
</div>
<script type="text/javascript">
var selectedIds = [];
$(document).ready(function () {
$("#divComment").dialog({
height: 150,
width: 400,
modal: true,
autoOpen: false,
draggable: false,
resizable: false,
open: function() {},
close: function(event, target) {
$("#divComment").html('');
return false;
}
});
...
//"Edit" button
$('#edit-selected').click(function (e) {
e.preventDefault();
//You can only edit one item at a time
if (selectedIds.length != 1) {
alert("You must select only one comment to edit");
return false;
}
//Get selectedId and set link
var selectedId = selectedIds[0];
var link = '@(Url.Action("ProductCommentAddPopup", "Product", new { Id = "-1",
btnId = "btnRefreshProductComments", formId = "product-form" }))';
link = link.replace("-1", selectedId);
$("#divComment").load(link);
$("#divComment").dialog("open");
//Open dialog for Comment
//OpenWindow(link, 400, 125, true);
return false;
});
...
});
</script>
Over in my view for the popup (ProductCommentAddPopup.cshtml):
@{
Layout = "~/Administration/Views/Shared/_AdminPopupLayout.cshtml";
}
@model ProductModel.ProductCommentsModel
@*@{
if (ViewBag.ShouldClose)
{
<script type="text/javascript">
try {
// $("#divComment").dialog("close");
window.opener.document.forms['@(ViewBag.formId)'].@(ViewBag.btnId).click();
window.opener.document.forms['@(ViewBag.formId)'].selectedIds = [];
window.close();
}
catch (e){}
// var url = $("#productcomments-grid").attr("url");
// jQuery.ajax({
// url: url,
// timeout: 2000,
// cache: false
// }, function () { });
</script>
}
}*@
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.CustomerId)
@Html.HiddenFor(model => model.ProductId)
@Html.HiddenFor(model => model.CreatedDate)
@Html.HiddenFor(model => model.LastModifiedDate)
<table class="adminContent">
<tr>
<td class="adminTitle">
@Html.NopLabelFor(model => model.Comment):
</td>
<td class="adminData">
@Html.TextAreaFor(model => model.Comment)
</td>
</tr>
<tr>
<td colspan="2">
<button id="save" name="save" value="save" class="t-button">@T("Admin.Common.Save")</button>
</td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#save").click(function (e) {
var formId = "@(ViewBag.formId)";
var btnId = "@(ViewBag.btnId)";
$.ajax({
cache:false,
type: "POST",
url: "@(Url.Action("ProductCommentAddPopup", "Product"))",
data: { "formId": formId, "btnId": btnId },
complete: function () { alert("here"); },
success: function (html) {
return false;
window.opener.document.forms['@(ViewBag.formId)'].@(ViewBag.btnId).click();
$("#divComment").dialog("close");
},
error:function (){
alert('Failed to add/update product comment.');
}
});
});
});
</script>
}
In ProductController.cs:
[HttpPost]
//[FormValueRequired("save")]
public ActionResult ProductCommentAddPopup(string btnId, string formId,
ProductModel.ProductCommentsModel model)
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
return AccessDeniedView();
var productComment = PrepareProductComments(model);
//Update a Product Comment
if (productComment.Id != 0)
_productService.UpdateProductComment(productComment);
//Adding a new Product Comment
else
_productService.InsertProductComment(productComment);
//Update the fields and set the close flag to true
ViewBag.btnId = btnId;
ViewBag.formId = formId;
ViewBag.ShouldClose = true;
return PartialView(model);
//return View(model);
}
The commented pieces are how I make it work with a popup window instead of the dialog.