将 .NET AJAX 转换为 MVC/学习 - 是否可以在不破坏 MVC 范例的情况下实现此功能?

发布于 2024-11-17 06:45:38 字数 2582 浏览 4 评论 0原文

我正在尝试在 MVC 中复制以下功能,我让它适用于 AJAX:

  • 用户将项目拖放到页面上。
  • OnDropped 将创建一个新控件并将其附加到页面。该控件的类型为 RadDock,它将停靠到 RadDockZone 类型的控件。

旧代码:

    /// <summary>
    /// Creates the RadDock + Contents when dropping a graph onto the page.
    /// </summary>
    /// <param name="sender"> The RadListBox with an element being dropped from inside it. </param>
    /// <param name="e"> Information about the drop such as target and what is being dropped. </param>
    protected void RadListBox_Dropped(object sender, RadListBoxDroppedEventArgs e)
    {
        CormantRadDockZone dockZone = RadDockLayout1.RegisteredZones.OfType<CormantRadDockZone>().First(registeredDockZone => registeredDockZone.ID == e.HtmlElementID);

        if (!object.Equals(dockZone, null) && !dockZone.Docks.Any())
        {
            RadSlidingPane slidingPane = ((sender as RadListBox).Parent as RadSlidingPane);
            CormantReport report = new CormantReport(int.Parse(e.SourceDragItems[0].Value), e.SourceDragItems[0].Text, slidingPane.Title);
            CormantRadDock dock = new CormantRadDock(report);
            dock.CreateContent();
            dock.Dock(dockZone);
        }
    }

现在,我一直在努力思考如何在 MVC 中做这样的事情。我认为应该发生的是视图应该获取必要的数据,然后将该数据传递回控制器。控制器将处理创建 RadDock,然后将对象转换为 JSON 并将数据传递回视图。检索该数据后,我“相信”我应该能够以某种方式重新创建该元素,以便我可以将其附加到 RadDockZone,但没有 Telerik.Web.UI.RadDock() 构造函数......这似乎相当无望? 这可能吗?

function OnClientDropped(sender, eventArgs) {
//Capture e.HTMLElementID, e.SourceDragItems[0].Value, e.SourceDragItems[0].Text, and slidingPane.Title here

var droppedID = eventArgs.get_htmlElement().id;

if( droppedID.indexOf("RadDockZone") != -1 )
{
    var radDockZone = $find(droppedID);
    //This constructor does not exist!
    var radDock = new Telerik.Web.UI.RadDock();
    radDock.dock(radDockZone);
}
else
{
    alert("Failed to find RadDockZone on dropped target.");
}

var slidingPaneTitle = "";

if (sender.get_id().indexOf("lstBxHistorical") != -1) {
    slidingPaneTitle = "Historical Widgets";
}
else if (sender.get_id().indexOf("lstBxCustom") != -1) {
    slidingPaneTitle = "Custom Widgets";
}
else {
    alert(sender.get_id());
}

$.ajax({
    type: 'POST',
    url: 'ReportDropped',
    dataType: 'json',
    data: {
        //e.HTMLElementID, e.SourceDragItems[0].Value, etc...
    },
    success: function (data) {

        alert("Success");
    },
    errror: function (xhr, ajaxOptions, error) {
        alert("Error");
    }
});

}

I am trying to replicate the following functionality in MVC, I have it working for AJAX:

  • User drag-and-drops an item onto the page.
  • OnDropped a new control is created and attached to the page. The control is of type RadDock and it will be docked to a control of type RadDockZone.

Old Code:

    /// <summary>
    /// Creates the RadDock + Contents when dropping a graph onto the page.
    /// </summary>
    /// <param name="sender"> The RadListBox with an element being dropped from inside it. </param>
    /// <param name="e"> Information about the drop such as target and what is being dropped. </param>
    protected void RadListBox_Dropped(object sender, RadListBoxDroppedEventArgs e)
    {
        CormantRadDockZone dockZone = RadDockLayout1.RegisteredZones.OfType<CormantRadDockZone>().First(registeredDockZone => registeredDockZone.ID == e.HtmlElementID);

        if (!object.Equals(dockZone, null) && !dockZone.Docks.Any())
        {
            RadSlidingPane slidingPane = ((sender as RadListBox).Parent as RadSlidingPane);
            CormantReport report = new CormantReport(int.Parse(e.SourceDragItems[0].Value), e.SourceDragItems[0].Text, slidingPane.Title);
            CormantRadDock dock = new CormantRadDock(report);
            dock.CreateContent();
            dock.Dock(dockZone);
        }
    }

Now, I have been trying to wrap my head around how to do something like this in MVC. What I -think- should happen is that the view should obtain the necessary data, then pass that data back to the controller. The controller would handle creating the RadDock, then toJSON the object and pass the data back to the view. After retrieving that data, I 'BELIEVE' I should be able to recreate the element in some way such that I can append it to the RadDockZone, but without a Telerik.Web.UI.RadDock() constructor...it seems fairly hopeless?
Is this possible?

function OnClientDropped(sender, eventArgs) {
//Capture e.HTMLElementID, e.SourceDragItems[0].Value, e.SourceDragItems[0].Text, and slidingPane.Title here

var droppedID = eventArgs.get_htmlElement().id;

if( droppedID.indexOf("RadDockZone") != -1 )
{
    var radDockZone = $find(droppedID);
    //This constructor does not exist!
    var radDock = new Telerik.Web.UI.RadDock();
    radDock.dock(radDockZone);
}
else
{
    alert("Failed to find RadDockZone on dropped target.");
}

var slidingPaneTitle = "";

if (sender.get_id().indexOf("lstBxHistorical") != -1) {
    slidingPaneTitle = "Historical Widgets";
}
else if (sender.get_id().indexOf("lstBxCustom") != -1) {
    slidingPaneTitle = "Custom Widgets";
}
else {
    alert(sender.get_id());
}

$.ajax({
    type: 'POST',
    url: 'ReportDropped',
    dataType: 'json',
    data: {
        //e.HTMLElementID, e.SourceDragItems[0].Value, etc...
    },
    success: function (data) {

        alert("Success");
    },
    errror: function (xhr, ajaxOptions, error) {
        alert("Error");
    }
});

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2024-11-24 06:45:38

我让它适用于 AJAX:

我相信你指的是 aps.net ajax。并且您不能将asp.net的服务器控件与mvc一起使用,因为mvc根本没有任何asp.net服务器控件严重依赖的ViewState概念。如果您确实想使用这样的控件,您有两个选择(我能想到)

I have it working for AJAX:

i believe you mean aps.net ajax. And you can not use server controls of asp.net with mvc because mvc simply does not have any concept of ViewState on which asp.net server controls heavily rely. if you do want to use such a control you have two options (that i can think of)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文