Closing a window after a HttpPost action is fired successfully

8 posts, 0 answers
  1. louis
    louis avatar
    16 posts
    Member since:
    Nov 2011

    Posted 15 Nov 2012 Link to this post

    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.
  2. louis
    louis avatar
    16 posts
    Member since:
    Nov 2011

    Posted 18 Nov 2012 Link to this post

    Anyone?

  3. Kendo UI is VS 2017 Ready
  4. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 20 Nov 2012 Link to this post

    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!
  5. Daniel Abell
    Daniel Abell avatar
    7 posts
    Member since:
    Jul 2005

    Posted 18 Jan 2013 Link to this post

    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?
  6. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 23 Jan 2013 Link to this post

    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!
  7. Daniel Abell
    Daniel Abell avatar
    7 posts
    Member since:
    Jul 2005

    Posted 23 Jan 2013 Link to this post

    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
  8. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 28 Jan 2013 Link to this post

    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!
  9. Thomas
    Thomas avatar
    19 posts
    Member since:
    Aug 2013

    Posted 10 Oct 2013 Link to this post

    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")
Back to Top
Kendo UI is VS 2017 Ready