HTML and CSS Reference
In-Depth Information
int questionId = int.Parse(data[str]);
Result result = new Result();
result.QuestionID = questionId;
result.ChoiceID = choiceId;
result.UserID = usrId;
db.Results.AddObject(result);
}
db.SaveChanges();
return Json("success");
}
The
SaveResults()
method is bit lengthy and requires careful observation. In order to convert JSON
data passed from the client into a .NET dictionary, it uses the
Json.NET
library.
Json.NET
is a popular high-
performance JSON framework for .NET that offers flexible ways to convert between JSON and .NET types.
SaveResults()
first reads the
InputStream
of the request into a string variable. It then uses the
JsonConvert
class of the
Json.NET
library to convert the JSON string into a .NET dictionary. Because
localStorage
stores
all data as plain strings, the .NET dictionary uses string key and value data types
(
Dictionary<string,string>
).
n
Note
Json.NET
is a powerful open source framework and offer many other features. In this example, however,
you need it only to convert a JSON dictionary to a .NET dictionary. Visit
http://json.codeplex.com
for
more
details about
Json.NET
.
SaveResults()
then adds the user information to the
Users
table. The user's
UserID
is retrieved
because it's also required while adding records to the
Results
table. The three keys—
FirstName
,
LastName
,
and
Email
—are removed after a record is added in the
Users
table so that only
ChoiceID
keys are left. This
way, you can simply run a
for-each
loop and add data to the
Results
table.
Figure 7-8 shows a sample successful run of the Survey application.
Figure 7-8.
Sample run of the Survey application