Simple data collection application using JSON and .NET - c#

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

Related

Accessing JSON on server side (asp.net)

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.

Data stores in JavaScript or Jquery?

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

MVC3 & html form. how to get params from URL?

I haven't done web applications before, but I'm familiar with C#, and am trying to create a website using MVC3.
I have a page with an HTML form, i have it set to use GET method, and on submit to go to a new page.
The new page will load fine, and have the parameters in the URL like this:
http://localhost:55751/Home/ClassAdded?Semester_Fall=Summer&Ticket=123&Year=2012
On this new page, how do I get the parameters from the URL to work with them in C#? Essentially, I want to just take those params and put them in in a database.
Have a look at http://www.asp.net/mvc/pluralsight for a nice little introduction.
Easiest way would be to define your model and have a strongly typed view that will send an instance of the model to the controller. There you would (very easily) just store the model instance received to the database (use Entity Framework or a Linq Data Context for that, up to date people would use EF though).
There is no real magic, just get to know the MVC pattern and then how it is implemented in MVC 3 ...
ps: i don't think the question would earn a -1, sure (s)he could have had a go on google... but well shrug
I recomend you to read some book: http://www.amazon.com/Pro-ASP-NET-Framework-Steven-Sanderson/dp/1430210079. You need to use action parameters.

SQL data to JSON file

I have a question for you stackoverflow experts :). I'm planning to use the FLOT jQuery plugin to chart some data to the MVC3 application I'm building.
I have several views that return data, but I'm just using a resultset and not an entity framework of any sort (it's nice and fast). I've never worked with JSON before, and I have the following questions:
a) Do I have to use a data model to return data to the controller, then use the JSON method to create the JSON file, or can I return the data without using an entity framework and achieve the same result?
b) Is there a bit of sample code anywhere that can help me out with this (or a link to a good example?
Many thanks in advance ;-).
If you can get your data into a class with properties (get;set;) then you can simply use the built in serializer for MVC ie return Json(yourClass);
If you want a bit more control over it, check out
http://json.codeplex.com/
Theres a linq-to-json and a dataset converter as well.

MVC way of handling data input

I have a data input module where I add the information of my product and its sub information like:
product basic info
product price info
product price details
price info and price details are related to product and are lists
In my web forms approach I would store my main product object on the view state and I would populate it's pricing info and details while doing ajax postbacks. This way I can create a compact module that is very user friendly in terms of defining a lot of data from one place without the need to enter these data from seperate modules. And when I am done I would do one product.save() and that would persist all the data to the respective tables on db.
Now I am building similar app on .net mvc framework and pondering on what would be the good way of handling this on mvc.
I don't resonate towards storing all this on client side till I click save. And saving to the db after each action makes me remember the days I was coding on asp.
Will appreciate your inputs on ways to approach this on mvc framework
I believe the best way of doing this is storing the data on the client side. It reduces unnecessary postbacks and improves responsiveness of your application. If you really want to store it on the server, you can use SessionState.
If you really want to store it in something like ViewState, you can go with a solution like this: ASP.NET MVC - Is there a way to simulate a ViewState?. However, I recommend against it as it will make things more complicated. Doing it client-side is probably the most elegant way and storing it in SessionState is the easiest.
Remember that you can always escape the MVC pattern and use a simple Web form for that specific page (which will give you ViewState where you need it): ASP.NET MVC controller actions design
store your Product list to the Model of the view and each time you change a value you can do a Ajax post to the controller and save the changes to the db, use partial views to display each item in your product list
you can try to integrate http://www.castleproject.org/ActiveRecord/ for easy saving and updating. That way you can just map your Model on your database using ORM(Object Relational Mapping). It takes a bit more work in the beginning but you will end up with simple commands like product.Update() and product.Create()

Categories

Resources