I have a web app that we're still stuck in asp.net 2.5 with. We've started using some newer technology with our front end, including loading some of our data into JSON objects and passing them around the application using localStorage. It's working really great.
In the future we're going to change our ASP.NET web form architecture into ah HTML5/JQuery front and Web API back end. So we're trying to write for that future while still being constrained to our old web form post backs and business objects. So right now we're posting from our search form to our search result page web form and we'll be calling a method from our business object to grab and return search results.
The criteria object we pass in has 20 or so values and a couple of collections (product line ID's, category ID's, etc..). So it's a slightly complicated object. In the old form we grabbed values from the controls, validated them, and passed them in using the asp.net controls, it was a single form solution. Our new solution has a search form and a results page. We're passing our values from form to form in a JSON object in internal storage. I can't really get to that from server side so I also stashed the values in a hidden field on the form that I can grab on the server side when I POST to the results page (eventually we'll call an API from the new form using ajax). So now that I can see the data, how do I parse and work with a JSON object in the code behind of asp.net. I need to load the 20 or so search criteria values and iterate through the ID collections (Guid and int) to load them into the same criteria object. This object is then passed in as the search methods parameter and search results will come back. Not sure how to manipulate the json on the server side.
If I understand the question, you have a JSON string in the server, and you just need to work with it.
Easiest way is to define a class (or in this case, a few classes) representing the data, then simply deserialize the JSON into an instance of the class. From there, you've got a regular object, and can do whatever you need with it, including serializing it back to JSON if you want.
With JSON.NET (aka Newtonsoft.Json), it's as simple as:
var myObject = JsonConvert.DeserializeObject<SomeType>(jsonString);
If you need help building the class, you can use json2csharp, where you can pass in a sample JSON file, and it builds the appropriate C# classes for you.
You can use DataContractJsonSerializer class, if the .Net framework is upgrade to 3.5 or later.
But, I found this article in web, http://www.netfxharmonics.com/2008/01/DojrNET-Dojo-RPC-Library-NET-20,
which is using Dojr.Net library and its compatible with .Net 2.0.
Hope this helps.
Related
I have to implement a server side code to a Form Builder front end component which allows to create Forms through drag and drop, something like in this link
When the form is created a JSON is generated and has to be stored in the database. From there, then there are two uses, first is to edit an already stored form JSON, and second is to implement the Form in html view using the JSON, so that the users can consume it. The form JSON looks something like this, although the levels get deeper for some form components.
[{"name":"UserForm"},
{"id":"txtID","label":"Your Name","placeholder":"your name","helptext":"Please enter your name","required":true,"inputsize":"input-xlarge"},
{"id":"selectID","options":["Cricket","Football"],"inputsize":"input-xlarge","label":"Favorite Game"}]
When storing this JSON, I can see two ways and which one to choose is confusing me. Firstly, I can create entity classes representing the JSON, and also separate SQL Server Tables for these entities. So the idea is to create complete entities with relationships representing the JSON hierarchy.
Another is to store the complete JSON in one SQLServer Table string (or similar) field and work on that string for processing.
The first option seems to be fragile, since if the JSON keys change, the database schema has to be updated. The second option can absorb this without requiring any changes in database. But, I am not sure of how to process JSON string as objects in C# code. I will need this for implementing say, Validation Logic for any form implementation. So, for example if a "Name" key has a value of "Required" in JSON, then it has to be validated on server side using c#.
So in short how store JSON, as a string field or as Entities representing the JSON hierarchy.
I'm building a web application that consists only of one page (Literally one page, no HTML generation). The whole purpose of this application is to collect data from users and then save it into a SQL Server database.
I want to use Javascript to validate and collect the data from the HTML Form, put it in a JSON object, and then use C# and .NET to insert the data into SQL Server.
I have no clear idea on how to achieve this, and honestly, I'm not really sure if this is a valid model or not!
Any pointers or ideas on what to look for and where to start? And how can I achieve this in the simplest way possible?
I would suggest using jQuery and ASP.NET MVC. Here is an example:
Posting simple json to MVC action controller method with jquery
or this one:
Send data to MVC controller using JSON
I am developing a site which retrieve few lists (like country, product, industry) from share point using a soap call, and load the drop-downs. Later there are few filtration and reload of country dropdown based upon selected region.
Instead of making a soap call each time I was thinking if I can use java-script to once retrieve all the lists and store it on client side and reference it to reload the drops downs, something like List<>, Dictionary<> or hash tables in c#, something which can support key/value concept.
Please suggest.
Thanks in advance.
I would recommend using JSON (JavaScript Object Notation) to keep your object on the browser side. Here's a link to an example of getting data from the server and using JSON on the browser side http://www.codeproject.com/Articles/211489/Using-JSON-with-ASP-NET-3-5
I have a pure jQuery app that keeps it's objects in JSON format and provides client-side editing. I want to persist the JSON to a database, but everything seems like overkill (like object mapping in C#). I am close to settling on storing the whole JSON string into a single column of a database. I was hoping there was more of an elegant, creative approach like a jQuery/JSON/database framework out there, but couldn't find anything (maybe NoSql can be of use?).
Any suggestions or advise on how to persisting JSON to a database using as less server code as possible?
I would check out MongoDB due to it's JSON style documents.
It also has plenty of drivers, so I wouldn't see it being an issue.
I assume it's ok to use a server side technology, your description wasn't real clear for me.
The first thing that comes to mind for your needs is MongoDB (http://www.mongodb.org/). It sports a javascript shell and objects that are natively represented in JSON (BSON technically).
The server supports indexing and other things that give you the warm SQL fuzzies.
What about CouchDB? It uses JSON notation to store the objects and you could access the db directly from the client side via its REST interface.
Besides, there are already several jquery plugins for CouchDB, jqCouch just to mention one.
I work on a web application that modifies a XML document that is stored in a database. It is made to mimic exactly like any program does when you click the "preferences" option in the menu. The XML document is read by a local client application and any changes are reflected in the local client.
My web app has 3 tiers I guess you would say with the aspx page being the "view", a service layer where I validate / process all user input, and a data layer where I update the XML document.
For each page of settings, I had been recursively traversing all the controls on the page and if I came to one that I wanted to work on (checkbox, textbox, ...) I added it to a IList and then sent that IList to the service layer where I then had to pull the control out of the list so I could work on it.
I noticed this seemed to be a bit slow so I profiled the page and basically the LINQ to Objects queries that I had been using tend to eat up a ton of time.
(CheckBox)lstControls.Where(x => x.ID == "some_id").SingleOrDefault();
I then switched to manually adding the controls on the page to a IList and then pulling them out in the service layer by indexer in the order they were put in. This is fugly and completely dependent on you not screwing up the index of the control you are looking for.
Finally, this breaks the rule of mixing elements from the view with the service layer or data layer. I know I should be working with data only, but I am at a loss of how to efficiently do this.
Each settings page has from one to thirty controls that need to be processed. How do I get all the data from the controls to the service layer without sending the actual controls?
Thanks for the help....
You might get into a better situation if you gather the data, the stuff you are actually interested in, into some object structure with some semantics that will help you structure what is passed on to the next layer. The keyword "Databinding" should also help you to just get to the values that were entered. There is no need to pass any controls back to the next layer..