First we need a column command button in our grid.
.Columns(columns =>
{
//Columns here
columns.Command(commands =>
{
//Other commands
commands.Custom("viewDetails").ButtonType(GridButtonType.Text)
.Text("Detail");
}).Width(60);
})
This time we need a different event instead of OnRowSelect.
.ClientEvents(events => events.OnRowDataBound("OnRowDataBound"))
Then we need two javascript functions in script tags. First one will handle the OnRowDataBound event, and second one will handle OnClick event of our custom command button. Id is grid's routevalue, and we use custom command's id to locate our element, t-grid-viewDetails.
function OnRowDataBound(e) {
var id = e.dataItem.Id;
$(e.row).find('a.t-grid-viewDetails').attr('href', 'javascript:ViewDetails_OnClick(' + id + ');');
}
In the older version of this approach, we put window as invisible in the aspx page and then opened it in javascript function. Here, we will create window in the javascipt function and then open it.
As we create window in javascript, we no longer need hardcoded window in the aspx page, you can remove it.
function ViewDetails_OnClick(id) {
$.ajax({
type: 'POST',
url: '<%= Url.Action("Action", "Controller") %>',
data: { Id: id },
dataType: 'html',
success: function (htmlData) {
var windowElement = $.telerik.window.create({
title: "...",
html: htmlData,
contentUrl: "",
actions: ["Maximize", "Close"],
modal: true,
width: 500,
height: 500,
resizable: false,
draggable: true,
scrollable: false,
onClose: function () {
$("#popupWindow").data('tWindow').destroy();
}
});
windowElement.attr('id', 'popupWindow');
var window = $(windowElement).data('tWindow');
window.center().open();
}
})
}
And at last, our action method did not change.
public ActionResult Action(int Id)
{
//Prepare model for PartialView
return PartialView("PartialViewName", model);
}
Above code pops up a detail window with your partial view when custom command is clicked.
Hope it helps.
P.S : If there is any typing or coding mistakes let me know. See you later.
Hello,
ReplyDeleteHow do you close the pop up window after it calls the http post Action method and refresh the grid?
I just updated that part.
ReplyDeleteYou should assign an ID for that window, in this case it is popupWindow. Then you should destroy that window in onClose event.
Hope it helps.
Regards.
Hi Again, thanks for the tip. But what does your corresponding http post method look like? What does it return, I'm returning Content("") but when my window closes it goes to my partial view url. I want to close the popup window and rebind the telerik grid.
ReplyDelete[HttpPost]
public ActionResult Action(YourViewModel model)
{
return Content(""); //not sure what to return to close the window and rebind grid
}
To be clear, in my partial view I have a Ajax.BeginForm() that posts to my HttpPost action. Inside that action, what do I return? For some reason a blank page with the url to my partial view is rendered.
DeleteHi,
DeleteActually I just return partial view to ajax call made in ViewInPopup function.
-->return PartialView("MyPartialView", model);
You can return Json in a similar fashion. You just have to render partial view into string first then pass it to a variable in json.