How to save and load diagram from Database in ASP.NET MVC?
Diagram can be saved and loaded back from database.
- Save method of diagram returns a serialized JSON object that contain the current state of the diagram.
- You can convert the JSON object into a string and it can be stored anywhere (file, database, etc...)
- This string can be converted back to JSON object and loaded using Load method.
- The same methodology can be used for both ASP.NET and ASP.NET MVC application.
The following section explains you on how to implement the above.
ASP.NET MVC
In ASP.NET MVC, save and load in database is performed using AJAX Post method. The following code illustrates how to save the diagram in database.
Save
CSHTML
function save() { var diagram = null; var diagram = $("#diagram1").ejDiagram("instance"); //returns diagram model’s json data var save = diagram.save(); //convert JSON object to JSON string var Jsonstring = JSON.stringify(save); //pass JSON string to server via AJAX post $.ajax({ url: "/Entity/save", type: "Post", dataType: "json", data: { "stringify": Jsonstring }, success: function (Jsonstring) { alert("Success"); return true; }, });
The following HttpPost is executed when the jQuery AJAX call is made from client side. This method accepts the JSON string from client side and it is inserted in the database.
CONTROLLER
[HttpPost] public ActionResult save(string stringify) { NorthwindcontextsDiagramEntities entities = new NorthwindcontextsDiagramEntities(); SaveDatabase SaveJSON = new SaveDatabase(); SaveJSON.JSONString = stringify; SaveJSON.Name = "Sam"; entities.SaveDatabases.Add(SaveJSON); entities.SaveChanges(); return null; }
The following code illustrates how to load the diagram from the database.
Load
CSHTML
function load() { $.ajax({ url: "/Entity/load", type: "Post", success: function (Jsonstring) { var diagram = null; var diagram = $("#diagram1").ejDiagram("instance"); //convert JSON string to JSON object var loadDiagram = JSON.parse(Jsonstring); //load the diagram using saved json data diagram.load(loadDiagram); }, }); }
The following HttpPost is executed when the jQuery AJAX call is made from client side. This method retrieves the JSON string from the database and send to client side.
CONTROLLER
[HttpPost] String name,jsonstring; public ActionResult load() { NorthwindcontextsDiagramEntities entities = new NorthwindcontextsDiagramEntities(); entities = SqlCE; List<SaveDatabase> loadJSON = entities.SaveDatabases.ToList<SaveDatabase>(); foreach (SaveDatabase data in loadJSON) { jsonstring = data.JSONString; name = data.Name; } string loadcontent= jsonstring; return Content(loadcontent); }
Here is the sample of ASP.NET MVC
Sample:https://www.syncfusion.com/downloads/support/forum/121121/ze/saveloadsample1583379421
ASP.NET
In ASP.NET, save and load in database is performed using AJAX PageMethods. PageMethods is a new mechanism in ASP.NET applications where the server code can be bound to ASP.NET pages. It is an easy way to communicate asynchronously with the server using ASP.NET AJAX technologies. It is an alternate to calling the page that is reliable and carries a low risk. It is basically used to expose the web methods to the client script.
Set EnablePageMethods=”true” in ScriptManger tag that allows to call a web page method directly from JavaScript.
ASPX
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
The following code illustrates how to save the diagram in database.
Save
ASPX
function save() { var diagram = null; var diagram = $("#Diagram1").ejDiagram("instance"); //returns diagram model’s json data var saving = diagram.save(); //convert JSON object to JSON string var stringify = String(JSON.stringify(saving)); PageMethods.save(stringify); }
The following WebMethod is executed when the AJAX PageMethods call is made from client side. This method accepts the JSON string from client side and it is inserted in the database.
ASPX.CS
[WebMethod] public static void save(string stringify) { NorthWindDataContextDiagramEntities entities = new NorthWindDataContextDiagramEntities(); SaveDatabase SaveJSON = new SaveDatabase(); SaveJSON.JSONString = stringify; SaveJSON.Name = "Sam"; entities.SaveDatabases.Add(SaveJSON); entities.SaveChanges(); }
The following code illustrates how to load the diagram from the database.
ASPX
function load() { PageMethods.load(OnSuccess); } function OnSuccess(stringify) { var diagram = null; var diagram = $("#Diagram1").ejDiagram("instance"); //convert JSON string to JSON object var loaddiagram = JSON.parse(stringify); //load the diagram using saved json data diagram.load(loaddiagram); }
The following HttpPost is executed when the AJAXPageMethods call is made from client side. This method retrieves the JSON string from the database and send to client side.
ASPX.CS
[WebMethod] String name,jsonstring; public static string load() { NorthWindDataContextDiagramEntities entities = new NorthWindDataContextDiagramEntities(); entities = SqlCE; List<SaveDatabase> loadJSON = entities.SaveDatabases.ToList<SaveDatabase>(); foreach (SaveDatabase data in loadJSON) { jsonstring = data.JSONString; name = data.Name; } string loadcontent = jsonstring; return loadcontent; }
Conclusion
I hope you enjoyed learning about how to save and load diagram from Database in ASP.NET MVC.
You can refer to our ASP.NET MVC Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!