PDA

View Full Version : [CLOSED] Populating a Form Panel from a DirectMethod



ptrourke
Sep 23, 2013, 9:12 PM
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!

Daniil
Sep 24, 2013, 3:57 AM
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.

ptrourke
Sep 24, 2013, 8:55 PM
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.]

Daniil
Sep 25, 2013, 3:57 AM
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.

ptrourke
Sep 25, 2013, 3:16 PM
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 ...

ptrourke
Nov 08, 2013, 4:21 PM
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/1153385/a-circular-reference-was-detected-while-serializing-an-object-of-type-subsonic

Please keep this thread closed, but I thought it worthwhile to point out the source of the problem for future users.

Daniil
Nov 11, 2013, 3:45 AM
Thank you for sharing!