[CLOSED] Populating a Form Panel from a DirectMethod

  1. #1

    [CLOSED] Populating a Form Panel from a DirectMethod

    If I have a direct method that just returns an object, like "Customer", and I want to display those values in a FormPanel, can I do it like this:

    // C# Code
    
    public class MyType {
        public int    FirstAttribute {get; set;}
        public string SecondAttribute  {get; set;}
    
        public MyType(int myParameterID)
        {
            FirstAttribute = myParameterID;
        }
    }
    
    //Codebehind 
    
    [DirectMethod]
    public MyType MyDirectMethod(int myParameterID)
    {
        MyType myObject = new MyType(myParameterID);
        myObject.SecondAttribute = "Just some string";
        return MyType;
    }
    //Javascript 
    
    var MyListeners = {
          PopulateForm: function() {
               var myParameterID = 1;
               App.direct.MyDirectMethod(myParameterID, { success: function(myResult) {
                    App.MyForm.loadRecord(myResult);
                   } 
               });
           } 
    }
    <ext:FormPanel ID="MyForm" runat="server">
        <Items>
            <ext:FieldSet runat="server" ID="MyFieldSet">
                <Items>
                    <ext:DisplayField runat="server" ID="NameOfMyDisplayField" DataIndex="MyFirstAttribute" />
                    <ext:DisplayField runat="server" ID="NameOfMyDisplayField" DataIndex="MySecondAttribute" />
                </Items>
            </ext:FieldSet>
        </Items>
    </ext:FormPanel>
    <ext:Button ID="DoPopulate" runat="server">
        <Listeners>
            <Click Fn="MyListeners.PopulateForm" />
        </Listeners>
    </ext:Button>
    I ask because I can't debug inside the JavaScript DirectMethod caller, and I'm not getting the results I expect.

    Thanks!
    Last edited by Daniil; Oct 04, 2013 at 5:51 AM. Reason: [CLOSED]
  2. #2
    Hi @ptrourke,

    I think you can use the setValues method. Here is an example. Please note that I use Name instead of DataIndex. By default Name equals ID.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        public class MyType
        {
            public int FirstAttribute { get; set; }
            public string SecondAttribute { get; set; }
    
            public MyType(int myParameterID)
            {
                FirstAttribute = myParameterID;
            }
        }
    
        [DirectMethod]
        public MyType MyDirectMethod(int myParameterID)
        {
            MyType myObject = new MyType(myParameterID);
            myObject.SecondAttribute = "Just some string";
            
            return myObject;
        }
    </script>
    
    <!DOCTYPE html>
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    
        <script>
            var MyListeners = {
                PopulateForm: function () {
                    var myParameterID = 1;
    
                    App.direct.MyDirectMethod(myParameterID, {
                        success: function (myResult) {
                            App.MyForm.getForm().setValues(myResult);
                        }
                    });
                }
            };
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <ext:FormPanel ID="MyForm" runat="server">
                <Items>
                    <ext:FieldSet runat="server">
                        <Items>
                            <ext:DisplayField runat="server" Name="FirstAttribute" />
                            <ext:DisplayField runat="server" Name="SecondAttribute" />
                        </Items>
                    </ext:FieldSet>
                </Items>
            </ext:FormPanel>
    
            <ext:Button runat="server" Text="Populate">
                <Listeners>
                    <Click Fn="MyListeners.PopulateForm" />
                </Listeners>
            </ext:Button>
        </form>
    </body>
    </html>
    If you do want use the loadRecord method, you should define an <ext:Model>, create a record from values, then pass it to a loadRecord call.
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi @ptrourke,

    I think you can use the setValues method. Here is an example. Please note that I use Name instead of DataIndex. By default Name equals ID.
    Thanks, Daniil. That works with my sample test code.

    When I moved to my production code, I ran into a problem with LINQ. There is a foreign key in my table. This means that there is an attribute in the LINQ type for the child table that represents the type stored in the parent table.

    I'm talking about this feature: given two tables,

    PTRType
    PTRCategoryID {FK}
    PTRTypeID
    PTRTypeName
    PTRSeq

    PTRCategory
    PTRCategoryID
    PTRCategoryName

    if you have an instance of PTRType called MyType, you can navigate to
    MyType.PTRCategory.PTRCategoryName
    .

    It is this MyType.PTRCategory property to which I refer.

    If I just load the data normally and pass the instance of MyType from the DirectMethod to myResult unchanged, none of the values appear in the DisplayFields.

    HOWEVER, if I copy the data into another instance, and set
    MyType.PTRCategory = null
    , the values DO appear in the DisplayFields.

    Is there some setting in Ext.NET that I'm missing that would allow
    getForm().setValues()
    to ignore these complex-type attributes?

    Thanks!

    PTR

    [In case it's not clear, I have a functioning workaround; but I'd like to know the right way to do it.]
    Last edited by ptrourke; Sep 24, 2013 at 9:27 PM.
  4. #4
    What do you see in "myResult" in the first case (without setting up MyType.PTRCategory to null)?

    If you can provide a runnable test case, it would be nice to try it.
  5. #5
    Quote Originally Posted by Daniil View Post
    What do you see in "myResult" in the first case (without setting up MyType.PTRCategory to null)?

    If you can provide a runnable test case, it would be nice to try it.
    I will if I can reproduce in a test case. I say this because I just ported the same result to my other formpanel, and it works without setting the linked values to null. This suggests that my analysis of the issue is a tiny bit flawed ...
  6. #6

    Update

    I found the problem here, finally. The example I gave was a simplified one; in the real data model, there is a circular reference, and JSON cannot resolve the circular reference.

    http://stackoverflow.com/questions/1...-type-subsonic

    Please keep this thread closed, but I thought it worthwhile to point out the source of the problem for future users.
  7. #7
    Thank you for sharing!

Similar Threads

  1. [CLOSED] Example Populating Panel's Content (HTML required for SEO)
    By michaeld in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: May 03, 2013, 11:59 AM
  2. Replies: 0
    Last Post: Apr 10, 2013, 10:48 AM
  3. Replies: 5
    Last Post: May 11, 2012, 4:56 PM
  4. [Razor] Populating form values from controller
    By machinableed in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: May 07, 2012, 10:42 AM
  5. Replies: 5
    Last Post: Jan 04, 2011, 8:09 PM

Tags for this Thread

Posting Permissions