Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to Get DiagramLink or DiagramNode to MVC Controller Method (Read 3265 times)
Megan1717
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 39
Joined: Jun 20th, 2016
How to Get DiagramLink or DiagramNode to MVC Controller Method
May 8th, 2020 at 7:21pm
Print Post  
Hello,

I am having difficulty sending MindFusion data from client side to a MVC Controller method.

On the client side, I am getting a DiagramLink or DiagramNode when it is created by the user from the appropriate event handler. For instance, on the event for creating a link, I make an AJAX call trying to pass the new diagram link:

Code (Javascript)
Select All
function onLinkCreatedByUser(sender, args)
{
      var diagramLink = args.getLink();
       $.ajax({
                type: "POST",
                url:  urlHere,
                data: {  newDiagramLink: diagramLink },
                success: function (result) {
                    alert("success");
                },
                error: function (req, status, error) {
                    alert(error);
                }
            });
}
 



And my controller method signature looks like this:
Code (Java)
Select All
  [HttpPost]
  public ActionResult UserCreatedNewLink(DiagramLink newDiagramLink){
//do business logic
}
 



If I take out the parameter for the DiagramLink object, the AJAX call works. Or, if I instead pass the diagram as a JSON string, it also works.

However, I have also tried passing a ShapeNode or PointCollection, which are both MindFusion classes, but these also cause the AJAX call to fail.

Do you have a suggestion for being able to pass MindFusion data from the client side to a MVC controller method?

Trying to turn the diagram link in JSON with JSON.stringify also fails for the diagram link variable as a "circular JSON call," because that diagram link variable contains the diagram as part of the object. At least that's what the error complains about is the diagram property being JSON and it can't JSONify something that is already JSON. 

Thanks in advance.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #1 - May 11th, 2020 at 7:22am
Print Post  
Hi,

JSON.stringify(link.toJson()) should work. Link's own JSON alone, without any information about the nodes it connects, might not be of much help on server side. If you need to update some database tables to show nodes are now connected, you could send the ID values of link's Origin and Destination nodes instead.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Megan1717
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 39
Joined: Jun 20th, 2016
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #2 - May 11th, 2020 at 2:48pm
Print Post  
Thanks a lot! This does result in the AJAX call completing successfully.

My next question is how to deserialize the JSON string on the server side. I don't see a straightforward FromJson method like the DiagramView has. I see a DiagramLinkConverter class with a Deserialize method. Would this be the method I need? If so, can you provide an example of how to use this and what to pass in as parameters? The JSONified link comes into the controller as a string, so I don't see how I can match this with the expected params (dictionary<string, object>, Type, JS serializer).

I also see a LoadFromJson method on the DiagramLink class's server side reference. If this is the method I should use, could you please provide an example of how it would work?

Basically, I need to somehow end up with a DiagramLink object like we can for DiagramView and thus have access to the properties of the DiagramLink class.

Thanks.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #3 - May 12th, 2020 at 6:44am
Print Post  
Hi,

Try this -

Code
Select All
using System.Web.Script.Serialization

public ActionResult LinkCreated(string json)
{
    DiagramView view = new DiagramView();
    JavaScriptSerializer jss = new JavaScriptSerializer(new ItemTypeResolver(null));
    jss.RegisterConverters(new JavaScriptConverter[] { new DiagramLinkConverter(view) });
    DiagramLink link = jss.Deserialize<DiagramLink>(json);
    .... 



It should then let you read control point locations and appearance attributes from .NET object. If you need to know anything about Origin and Destination nodes, you will need to add some more JSON fields identifying them and then parse them yourself, or send an array of JSON objects, containing the link's JSON and some node values (database IDs, Tag / Text or whatever you identify nodes by).

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Megan1717
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 39
Joined: Jun 20th, 2016
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #4 - May 16th, 2020 at 6:04pm
Print Post  
Thanks! So the good news is that this works with diagram links that do not have a brush style. Unfortunately, if a link does have a brush style, I get the following error message during the deserialization line of code (shown below):

"No parameterless constructor defined for type MindFusion.Drawing.Brush".


DiagramLink link = jss.Deserialize<DiagramLink>(json);
  
Back to top
 
IP Logged
 
Megan1717
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 39
Joined: Jun 20th, 2016
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #5 - May 16th, 2020 at 6:12pm
Print Post  
I realized I should be more specific about the brushes I am referring to. So if a link has "null" for Base Brush and Head Brush, deserialization works just fine. If, however, these attributes have a value, deserialization will fail with the error message in my previous post.

Thanks.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #6 - May 18th, 2020 at 11:17am
Print Post  
That will work if you also register some converters for Brush etc types, but at this point you'll be better off submitting and loading this as diagram JSON -

Code
Select All
if (link)
{
    var json = { "measureUnit": 6, diagramBounds: diagram.getBounds() }
    json.items = [link.toJson()];

    $.ajax({
        type: "POST", url: "/Home/LinkCreated",
        data: { json: JSON.stringify(json) }, dataType: 'json',
    ....
}

public ActionResult LinkCreated(string json)
{
    DiagramView view = DiagramView.FromJson(json);
    Diagram diagram = view.Diagram;
    DiagramLink link = diagram.Links[0];
} 



Regards,
Slavcho
  
Back to top
 
IP Logged
 
Megan1717
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 39
Joined: Jun 20th, 2016
Re: How to Get DiagramLink or DiagramNode to MVC Controller Method
Reply #7 - May 18th, 2020 at 5:44pm
Print Post  
Thanks, Slavcho.

I was able to get the deserialization call to work if I set the brush properties to null on the client side.

Thanks for your help.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint