代码之家  ›  专栏  ›  技术社区  ›  Vogel612

使用扩展面板加载程序加载ASP-MVC视图

  •  1
  • Vogel612  · 技术社区  · 11 年前

    我有以下布局,与我在中描述的布局有关 this question

    <ext:Viewport runat="server">
      <Items>
        <ext:Panel Region="North" runat="server" ID="TopBar" />
        <ext:Panel Region="West" runat="server" ID="Nav" >
           <Items>
              <ext:Button runat="server" ID="loaderComponentFooBar">
                 <DirectEvents>
                    <Click OnEvent="loadFooBar" />
                 </DirectEvents>
              </ext:Button>
              //Some more Buttons each with his own loader Function in Code-Behind
           </Items>
        </ext:Panel>
        <ext:Panel runat="server" Region="center" ID="MainPanel" />
      </Items>
    </ext:Viewport>
    

    点击按钮,我现在想将我的View@“/Foo/Bar/”加载到 MainPanel

    我如何在CodeBehind中实现这一点?

    我已经试过了:

    public void loadFooBar(object sender, DirectEventArgs e)
    {
       MainPanel.Loader.Url = "/Foo/Bar/"; //Throws null reference Exception
    
       MainPanel.Loader.Url = "~/Foo/Bar/"; // same as above
    
       MainPanel.Loader.GetUrlByRoute("Foo/Bar/"); //again the same
    
       MainPanel.Loader.LoadContent("/Foo/Bar/");
    
       //and after each of these attempts except the last went:
       MainPanel.Loader.Mode = LoadMode.Frame;
       MainPanel.Loader.LoadMask.ShowMask = false;
       MainPanel.Loader.LoadContent();
    }
    

    顺便说一句,我正在使用aspx web应用程序视图

    1 回复  |  直到 7 年前
        1
  •  4
  •   Daniil Veriga    11 年前

    我认为您不应该将页面的代码隐藏用于DirectEvents的处理程序。它正在脱离MVC概念。您应该参考控制器的操作。

    这是一个例子。

    示例视图

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Ext.Net.MVC v2 Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
    
        <ext:Viewport runat="server" Layout="BorderLayout">
            <Items>
                <ext:Panel runat="server" Region="West" Width="100">
                    <Items>
                        <ext:Button 
                            runat="server" 
                            Text="Load View 1" 
                            DirectClickUrl=<%# Url.Action("LoadView1") %>
                            AutoDataBind="true" />
    
                        <ext:Button 
                            runat="server" 
                            Text="Load View 2" 
                            DirectClickUrl=<%#Url.Action("LoadView2") %>
                            AutoDataBind="true" />
                    </Items>
                </ext:Panel>
    
                <ext:Panel ID="MainPanel" runat="server" Region="Center">
                    <Loader runat="server" Mode="Frame" AutoLoad="false">
                        <LoadMask ShowMask="true" />
                    </Loader>
                </ext:Panel>
            </Items>
        </ext:Viewport>
    </body>
    </html>
    

    控制器示例

    using System.Web.Mvc;
    using Ext.Net;
    using Ext.Net.MVC;
    
    namespace Work2MVC.Controllers
    {
        public class AspxController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult View1()
            {
                return View();
            }
    
            public ActionResult View2()
            {
                return View();
            }
    
            public ActionResult LoadView1()
            {
                Panel p = X.GetCmp<Panel>("MainPanel");
                p.LoadContent(Url.Action("View1"));
    
                return this.Direct();
            }
    
            public ActionResult LoadView2()
            {
                Panel p = X.GetCmp<Panel>("MainPanel");
                p.LoadContent(Url.Action("View2"));
    
                return this.Direct();
            }
        }
    }
    

    此外,您还可以通过JavaScript在客户端启动加载请求。然后你就可以避免额外的请求了。