This is a migrated thread and some comments may be shown as answers.

Closing a window after a HttpPost action is fired successfully

7 Answers 952 Views
Window
This is a migrated thread and some comments may be shown as answers.
louis
Top achievements
Rank 1
louis asked on 15 Nov 2012, 03:59 PM
In my MVC project, I have a view which displays a list of roles (as in user roles), and an add button, which pops up a KendoUI window, rendering a Create view.
This is how my window snippet looks like:
@(Html.Kendo().Window()
           .Name("window")
           .Title("Role")
           .Content("loading...")
           .LoadContentFrom("Create""RolesPermissions", Model.Role)
           .Modal(true)
           .Width(550)           
           .Height(300)                      
           .Visible(false)
          )

And this is what I use to open(popup) and close the window
    $(document).ready(function () {
        var wnd = $("#window").data("kendoWindow");
 
        wnd.bind("refresh"function (e) {
            var win = this;
            $("#close").click(function() {
                win.close();
            });
        });
 
        $("#open").click(function (e) {
            wnd.center();
            wnd.open();
        });
 
    });

My question is, how do I close the window, if and only if the action is successful?

        [HttpPost]
        public ActionResult Create(RoleModel model)
        {
 
            if (ModelState.IsValid)
            {
                RoleDto role = new RoleDto
                    {
                        Name = model.Name,
                        Description = model.Description
                    };
 
                var roleAdded = _rolePermissionsRepository.AddRole(role);
                if (roleAdded != null)
                {
                    //CLOSE KENDOUI WINDOW
                }
                else
                {
                    //PRINT ERROR MSG TO KENDOUI WINDOW
                }
            }
            return View();
        }

I would like to know how to handle the portions that I have commented.

Basically, if the role is added succesfully, I would like the window to close automatically, taking me back to my list of roles, which I will then update. If there's any kind of error, the window will remain open, showing an error msg.

7 Answers, 1 is accepted

Sort by
0
louis
Top achievements
Rank 1
answered on 18 Nov 2012, 04:42 PM
Anyone?

0
Daniel
Telerik team
answered on 20 Nov 2012, 05:41 PM
Hi,

You can pass a parameter to the with the error message through the ViewBag or ViewData. If the parameter is not null in View, set the value as content and set Visible to true. If the parameter is null, use your current Window configuration.

Kind regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Daniel
Top achievements
Rank 1
answered on 18 Jan 2013, 05:51 PM
I am having the same problem as well and if there is an error the Window transforms into a full screen which is not what I wanted.

                if (ModelState.IsValid)
                {
                    // TODO: Add insert logic here                 
                    TempData["message"] = "Update successful";
                    return RedirectToAction("Index");            
                }
                else
                {                 
                    ViewBag.Message = "Some error";
                    return View();  // <==== Causes window to change into full screen                   
                }

Are there any code examples on how to persist the window?
0
Daniel
Telerik team
answered on 23 Jan 2013, 02:57 PM
Hello,

I am not sure if I understand the exact scenario. Could you share the code you are using for the view? To show the Window only when there are errors, you can use code similar to the one in the snippet below:

Html.Kendo().Window()                       
    .Visible(!ViewData.ModelState.IsValid)
Kind regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Daniel
Top achievements
Rank 1
answered on 23 Jan 2013, 06:26 PM
I have a grid which has an edit button for each record.  When the edit button is clicked, I have a simple form with a date field on the Kendo window. However, when I enter an invalid date (such as "sss") and click submit, the Kendo window goes away and a full screen of the form appears which displays the validation error.  I would like the Kendo window to display the error rather than the full screen.

Thanks,

Dan
0
Daniel
Telerik team
answered on 28 Jan 2013, 02:09 PM
Hello again Dan,

In order to show the message in the Window, you should render the same view that was shown initially and set the Window visibility to true. From the description you provided it seems that the form is posted to a different action method that is used for the Window content. If that is the case, then you should return the initial view instead. 

Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Thomas
Top achievements
Rank 2
answered on 10 Oct 2013, 09:06 AM
did you add the jquery unobtrusive bundle to the bundleconfig?

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.unobtrusive-ajax.js"));

and dont forget to add the render method to the Layout.cshtml like:

@Scripts.Render("~/bundles/jqueryval")
Tags
Window
Asked by
louis
Top achievements
Rank 1
Answers by
louis
Top achievements
Rank 1
Daniel
Telerik team
Daniel
Top achievements
Rank 1
Thomas
Top achievements
Rank 2
Share this question
or