我正在尝试在MVC中复制以下功能,我让它为AJAX工作:
- 用户将项目拖放到页面上
- 删除后,将创建一个新控件并将其附加到页面。控件为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");
}
});
}
我为AJAX工作:
我相信你指的是aps.net ajax。您不能将asp.net的服务器控件与mvc一起使用,因为mvc根本没有任何ViewState的概念,而asp.net服务器控件在很大程度上依赖ViewState。如果你真的想使用这样的控件,你有两个选项(我能想到)
- 实现与asp.net mvc兼容的自己的控件
- 或者并行运行asp.net和mvc,并在必要时使用asp.net服务器控件