[CLOSED] Help with Drag & Drop in Partial View

Page 1 of 2 12 LastLast
  1. #1

    [CLOSED] Help with Drag & Drop in Partial View

    Hi

    I'm trying to implement Drag & Drop in a Partial View. I've managed to get Drag & Drop working in a 'normal' MVC environment but I'm having difficulty with Partial Views.

    This is a simplified example.

    My Controller:

    using Ext.Net;
    using Ext.Net.MVC;
    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace ExtSandpit.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
            public ActionResult GetUsers()
            {
                List<User> lstUsers = new List<User>();
                lstUsers.Add(new User(1000, "Jeff"));
                lstUsers.Add(new User(1001, "James"));
                return this.Store(lstUsers);
            }
            public ActionResult GetProjectUsers()
            {
                List<User> lstUsers = new List<User>();
                lstUsers.Add(new User(1000, "Vicky"));
                lstUsers.Add(new User(1001, "Sheila"));
                return this.Store(lstUsers);
            }
            public Ext.Net.MVC.PartialViewResult RenderPartialView(string viewName, string containerId)
            {
                Ext.Net.MVC.PartialViewResult r = new Ext.Net.MVC.PartialViewResult(containerId, RenderMode.AddTo);
                r.ViewName = viewName;
    
                return r;
            }
        }
    
        public class User
        {
            public User()
            {
            }
            public User(int id, string name)
            {
                ID = id;
                Name = name;
            }
            public int ID { get; set; }
            public string Name { get; set; }
            public string Test { get; set; }
        }
    }
    My main view:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        @{
            Layout = null;
        }
        <script>
            var loadTestView = function (viewName) {
                App.PnlCenter.clearContent();
                Ext.net.DirectMethod.request({
                    url: '@(Url.Action("RenderPartialView"))',
                    params: {
                        containerid: "PnlCenter",
                        viewName: viewName
                    },
                })
            };
    </script>
    <link rel='stylesheet' type='text/css' href='~/Content/Site.css' media='screen'>
    </head>
    <body>
        @Html.X().ResourceManager()
    
        @(Html.X().Viewport().AutoScroll(true).Layout(LayoutType.Border).Items(
            Html.X().Panel().Region(Region.North).Layout(LayoutType.HBox).Items(
            ),
            Html.X().Panel().Region(Region.West).Width(181).Items(
                Html.X().Panel().ID("MenuPanel").Layout(LayoutType.VBox).Items(
                    Html.X().Button().Text("Test View 1").ComponentCls("menu-button").CtCls("menu-button-small").Listeners(lst => { lst.Click.Handler = "loadTestView('DragDropTest')"; })
                    )
                ),
            Html.X().Panel().Region(Region.West).ID("PnlCenter").Width(800).Layout(LayoutType.Fit)
            .Loader(
                Html.X().ComponentLoader().AutoLoad(false).Mode(LoadMode.Script)
            ),
            Html.X().Panel().Region(Region.Center).Layout(LayoutType.Fit)
        ))
    </body>
    
    </html>
    My Partial View:

    <script>
        var notifyEnter = function (ddSource, e, data) {
            App.ProjectUsers.body.stopAnimation();
            App.ProjectUsers.body.highlight();
        };
    
        var notifyDrop = function (ddSource, e, data) {
            var selectedRecord = ddSource.dragData.records[0];
            App.ProjectUsers.store.insert(0, selectedRecord);
            App.ProjectUsers.store.update();
            return true;
        };
    </script>
    @(Html.X().Panel().Layout(LayoutType.HBox).Width(800).Items(
            Html.X().GridPanel().Title("Users").ID("Users").Width(200).Height(1000)
                .Store(Html.X().Store().ID("UsersStore")
                .Model(Html.X().Model().IDProperty("ID")
                        .Fields(
                            new ModelField("ID", ModelFieldType.Int),
                                new ModelField("Name")
                                )
                )
                    .Proxy(Html.X().AjaxProxy().Url(Url.Action("GetUsers")).Reader(Html.X().JsonReader().RootProperty("data")))
                )
                .ColumnModel(
                    Html.X().Column().Text("Username").DataIndex("Name").Flex(10)
                )
                    .View(Html.X().GridView()
                    .Plugins(Html.X().GridDragDrop().EnableDrop(false).DDGroup("gridDDGroup"))
                    )
                .SelectionModel(Html.X().RowSelectionModel().Mode(SelectionMode.Single)),
    
            Html.X().GridPanel().ID("ProjectUsers").Title("Project Users").ID("ProjectUsers").MarginSpec("0 0 0 20").Width(300).Height(1000)
                .Store(Html.X().Store().ID("ProjectUsersStore")
                    .Model(Html.X().Model().IDProperty("ID")
                            .Fields(
                            new ModelField("Name")
                                )
                        )
                    .Proxy(Html.X().AjaxProxy().Url(Url.Action("GetProjectUsers")).Reader(Html.X().JsonReader().RootProperty("data")))
                )
                .ColumnModel(
                        Html.X().Column().Text("Username").DataIndex("Name").Flex(10)
                        )
                .SelectionModel(Html.X().RowSelectionModel().Mode(SelectionMode.Single))
    
            )
    )
        @( Html.X().DropTarget()
        .Target("={#{ProjectUsers}.body}")
        .Group("gridDDGroup")
        .NotifyEnter(h => h.Fn = "notifyEnter")
        .NotifyDrop(h => h.Fn = "notifyDrop")
    )
    The problem is that I have removed the Html tags from the Partial View (which is what I understood I needed to do). So the DropTarget code is not recognised. There's probably a simple solution?

    Thanks

    Jeff
    Last edited by fabricio.murta; Mar 22, 2016 at 11:23 PM. Reason: no user feedback for 7+ days
  2. #2
    Hello Jeff!

    What about changing your line 50 on the partial view code to:

    .Target("App.ProjectUsers.body")
    I still see javascript errors on the effective drop action though, it would require a little more looking thru the sample to be sure what's going on.
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Hi Fabricio

    I'll freely admit that, at the moment, javascript is not my strong point!

    When I do that I get "Unable to get property 'body' of undefined or null reference" in the IE debugger against (App.ProjectUsers.body)?

    One other question. I'm developing this based on an example on the website. So I've included .EnableDrop(false) in my GridDragDrop definition but I don't understand why. What does EnableDrop do, if it's not the obvious?

    Thanks

    Jeff
  4. #4
    Hello Jeff!

    Don't worry, we're here to help, I am sure we both learn here every topic we discuss!

    And about the "Unable to get property 'body' of undefined or null reference", I didn't really expect it not to work for you, maybe you are renaming the 'App.' namespace somewhere, but I thought it would be very improbable in your test application. You "rename the App namespace" when you give ResourceManager the Namespace= setting a different value than default (that is App). It is also possible in Web.config's extnet tag.

    About EnableDrop(false) I believe it is correctly used, and its interpretation should be: "Enable dropping on the own container where the items can be dragged". In other words, lets you drag the items out of the first panel but not just drop them there, as you want it valid only to drop on the grid to the right.

    This setting makes sense to be true, for one example, when you are using drag-drop to reorder items in the grid, or move cell contents from one row (or column) to other cell on the same grid.
    Fabrício Murta
    Developer & Support Expert
  5. #5
    Thanks Fabricio

    I think we need to take a step back!

    I kind of got it working in a 'normal' environment, i.e. without the Partial View

    Here is the Controller, similar to above:

    using Ext.Net;
    using Ext.Net.MVC;
    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace ExtSandpit.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
            public ActionResult GetUsers()
            {
                List<User> lstUsers = new List<User>();
                lstUsers.Add(new User(1000, "Jeff"));
                lstUsers.Add(new User(1001, "James"));
                return this.Store(lstUsers);
            }
            public ActionResult GetProjectUsers()
            {
                List<User> lstUsers = new List<User>();
                lstUsers.Add(new User(1000, "Vicky"));
                lstUsers.Add(new User(1001, "Sheila"));
                return this.Store(lstUsers);
            }
        }
    
        public class User
        {
            public User()
            {
            }
            public User(int id, string name)
            {
                ID = id;
                Name = name;
            }
            public int ID { get; set; }
            public string Name { get; set; }
            public string Test { get; set; }
        }
    }
    and the View:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        @{
            Layout = null;
        }
        <script>
            var loadTestView = function (viewName) {
                App.PnlCenter.clearContent();
                Ext.net.DirectMethod.request({
                    url: '@(Url.Action("RenderPartialView"))',
                    params: {
                        containerid: "PnlCenter",
                        viewName: viewName
                    },
                })
            };
            var notifyDrop = function (ddSource, e, data) {
                var selectedRecord = ddSource.dragData.records[0];
                App.ProjectUsers.store.insert(0, selectedRecord);
                App.ProjectUsers.store.update();
                return true;
            };
    </script>
    <link rel='stylesheet' type='text/css' href='~/Content/Site.css' media='screen'>
    </head>
    <body>
        @Html.X().ResourceManager();
    
        @(Html.X().Viewport().AutoScroll(true).Layout(LayoutType.Border).Items(
            Html.X().Panel().Region(Region.North).Layout(LayoutType.HBox).Items(
            ),
            Html.X().Panel().Region(Region.West).Width(181),
            Html.X().Panel().Region(Region.West).ID("PnlCenter").Width(800).Layout(LayoutType.Fit).Items(
            Html.X().Panel().Layout(LayoutType.HBox).Width(800).Items(
                   Html.X().GridPanel().Title("Users").ID("Users").Width(200).Height(1000)
                       .Store(Html.X().Store().ID("UsersStore")
                       .Model(Html.X().Model().IDProperty("ID")
                               .Fields(
                                   new ModelField("ID", ModelFieldType.Int),
                                       new ModelField("Name")
                                       )
                       )
                           .Proxy(Html.X().AjaxProxy().Url(Url.Action("GetUsers")).Reader(Html.X().JsonReader().RootProperty("data")))
                       )
                       .ColumnModel(
                           Html.X().Column().Text("Username").DataIndex("Name").Flex(10)
                       )
                           .View(Html.X().GridView()
                           .Plugins(Html.X().GridDragDrop().EnableDrop(false).DDGroup("gridDDGroup"))
                           )
                       .SelectionModel(Html.X().RowSelectionModel().Mode(SelectionMode.Single)),
    
                   Html.X().GridPanel().ID("ProjectUsers").Title("Project Users").ID("ProjectUsers").MarginSpec("0 0 0 20").Width(300).Height(1000)
                       .Store(Html.X().Store().ID("ProjectUsersStore")
                           .Model(Html.X().Model().IDProperty("ID")
                                   .Fields(
                                   new ModelField("Name")
                                       )
                               )
                           .Proxy(Html.X().AjaxProxy().Url(Url.Action("GetProjectUsers")).Reader(Html.X().JsonReader().RootProperty("data")))
                       )
                       .ColumnModel(
                               Html.X().Column().Text("Username").DataIndex("Name").Flex(10)
                               )
                       .SelectionModel(Html.X().RowSelectionModel().Mode(SelectionMode.Single))
    
                   )
            ),
    
            Html.X().Panel().Region(Region.Center).Layout(LayoutType.Fit)
        )
        )
        @( Html.X().DropTarget()
            .Target("={#{ProjectUsers}.body}")
            .Group("gridDDGroup")
            .NotifyDrop(h => h.Fn = "notifyDrop")
        )
    </body>
    
    </html>
    Previously I thought I had got this to work but now I'm getting a javascript error. Can you help me to get this working, then we should be able to replicate the problem I'm experiencing when I transfer it to the Partial View environment.

    Thanks

    Jeff
  6. #6
    Hello Jeff!

    Let me suggest a new approach? I've based this from this example: WebForms Grid to Grid drag & drop.

    So, I only changed the view from your code. Controller keeps the same.

    
    @{
        ViewBag.Title = "index";
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        @{
            Layout = null;
        }
        <script>
            var loadTestView = function (viewName) {
                App.PnlCenter.clearContent();
                Ext.net.DirectMethod.request({
                    url: '@(Url.Action("RenderPartialView"))',
                    params: {
                        containerid: "PnlCenter",
                        viewName: viewName
                    },
                })
            };
    
            var getDragDropText = function () {
                return this.view.panel.getSelectionModel().getSelection()[0].data.Name;
            };
    
            var handleGridDrop = function (node, data, overModel, dropPosition) {
                var dropOn = overModel ? ' ' + dropPosition + ' ' + overModel.get('Name') : ' on empty view';
                Ext.net.Notification.show({
                    title: 'Drag cell',
                    html: 'Dropped ' + data.records[0].get('Name') + dropOn
                });
    
                return false;
            }
        </script>
        <link rel='stylesheet' type='text/css' href='~/Content/Site.css' media='screen'>
    </head>
    <body>
        @Html.X().ResourceManager();
    
        @(Html.X().Viewport().AutoScroll(true).Layout(LayoutType.Border).Items(
            Html.X().Panel().Region(Region.North).Layout(LayoutType.HBox).Items(),
            Html.X().Panel().Region(Region.West).Width(181),
            Html.X().Panel().Region(Region.West).ID("PnlCenter").Width(800).Layout(LayoutType.Fit).Items(
                Html.X().Panel().Layout(LayoutType.HBox).Width(800).Items(
                    Html.X().GridPanel().Title("Users").ID("Users").Width(200).Height(1000).Frame(true)
                    .Store(
                        Html.X().Store().ID("UsersStore")
                        .Model(
                            Html.X().Model().IDProperty("ID").Fields(
                                new ModelField("ID", ModelFieldType.Int),
                                new ModelField("Name")
                            )
                        )
                        .Proxy(Html.X().AjaxProxy().Url(Url.Action("GetUsers")).Reader(Html.X().JsonReader().RootProperty("data")))
                    )
                    .ColumnModel(
                        Html.X().Column().Text("Username").DataIndex("Name").Flex(10)
                    )
                    .View(
                        Html.X().GridView().Plugins(
                            Html.X().GridDragDrop().EnableDrop(false)
                        )
                        .Listeners(l =>
                        {
                            l.AfterRender.Handler = "this.plugins[0].dragZone.getDragText = getDragDropText;";
                            l.AfterRender.Delay = 1;
                        })
                    )
                    .SelectionModel(Html.X().RowSelectionModel().Mode(SelectionMode.Single)),
    
                    Html.X().GridPanel().ID("ProjectUsers").Title("Project Users").ID("ProjectUsers")
                    .MarginSpec("0 0 0 20").Width(300).Height(1000).Frame(true)
                    .Store(
                        Html.X().Store().ID("ProjectUsersStore")
                        .Model(
                            Html.X().Model().IDProperty("ID")
                            .Fields(
                                new ModelField("Name")
                            )
                        )
                        .Proxy(
                            Html.X().AjaxProxy().Url(Url.Action("GetProjectUsers"))
                            .Reader(Html.X().JsonReader().RootProperty("data"))
                        )
                    )
                    .ColumnModel(
                        Html.X().Column().Text("Username").DataIndex("Name").Flex(10)
                    )
                    .View(
                        Html.X().GridView().Plugins(
                            Html.X().GridDragDrop().EnableDrop(true).EnableDrag(false)
                        )
                        .Listeners(l =>
                        {
                            l.Drop.Fn = "handleGridDrop";
                        })
                    )
                    .SelectionModel(Html.X().RowSelectionModel().Mode(SelectionMode.Single))
                )
            ),
    
            Html.X().Panel().Region(Region.Center).Layout(LayoutType.Fit)
        ))
    </body>
    
    </html>
    If you like it, a hint: when you don't see something in MVC Examples Explorer, chances are you can find a WebForms one that is relatively easy to port in and use on MVC.

    You may not like the way it drops replacing records. I didn't dig into this -- the original example does not replace the dropped cell, but adds positioning it. There should be something else, but maybe that was intentional by you.

    If you want to fully handle the drop event (the example's handleGridDrop just provides additional visual feedback to the drop event), you can just bind the handleGridDrop function to the BeforeDrop event and return false from the function. If you return false, it will act as if the drop operation was aborted -- keeping the entry on the original grid and giving you the opportunity to dedicatedly copy the cell and add it on the target grid.

    I didn't test yet, but I am pretty confident this would work just fine on partial views!
    Fabrício Murta
    Developer & Support Expert
  7. #7
    Hi Fabricio

    Thanks very much indeed. That works fine in my test environment, up to a point. However, after dropping a record the top part of the GridPanels disappears.

    This is before dragging:

    Click image for larger version. 

Name:	Drag and drop 1.png 
Views:	46 
Size:	4.0 KB 
ID:	24464

    and this is after dragging 'Jeff'

    Click image for larger version. 

Name:	Drag and drop 2.png 
Views:	45 
Size:	1.9 KB 
ID:	24465

    I noticed this before with the previous method. Any ideas?

    Thanks

    Jeff
  8. #8
    Hmmm really strange. Some times it is discarding the title of the panel, and some other times it doesn't. I've closed and re-opened the sample, and it does not remove the title when dropping...

    Actually on IE11, it seems to drop the panel title when I drag+drop with developer tools open. If I close developer tools, refresh and drag items, the panel title is not removed on drop. Chrome always drops the panels' title! I'll give it a deeper look and return here with a solution, hopefully very soon!
    Fabrício Murta
    Developer & Support Expert
  9. #9
    Hi Fabricio

    Thanks for looking into this.

    From what I could see though it isn't just dropping the title it also 'loses' the top row of the grid. For example if I drag and drop Jeff it loses Vicky?

    Thanks

    Jeff
  10. #10
    Hello Jeff,

    As for this problem it is because of ID conflicting. IDProperty columns must be unique in the set or they'll just end up replacing existing ones.

    Give it a try, change Vicky and Sheila in your controller:
                lstUsers.Add(new User(1002, "Vicky"));
                lstUsers.Add(new User(1003, "Sheila"));
    Fabrício Murta
    Developer & Support Expert
Page 1 of 2 12 LastLast

Similar Threads

  1. [CLOSED] render partial view
    By sharmav1 in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 27, 2015, 2:36 PM
  2. [CLOSED] DOM Drag-Drop example initial drag height
    By electromorph in forum 3.x Legacy Premium Help
    Replies: 2
    Last Post: Jul 09, 2015, 12:36 PM
  3. Replies: 0
    Last Post: Jun 12, 2014, 12:11 PM
  4. [MVC] How to use a partial view in a window?
    By KBorkiewicz in forum 2.x Help
    Replies: 7
    Last Post: Nov 21, 2012, 11:11 PM
  5. [CLOSED] MultiSelect with drag and drop, Drop listener
    By Jurke in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jan 30, 2009, 8:25 AM

Posting Permissions