My web form (built with Bootstrap 3 and Angular.js 1.3) is lengthy and mildly dynamic. In other words, there are a few cases of tabular arranged input fields and other regular input fields with an associated "Add another" button, as well as some input fields that toggle (hide/show) based on the input or selection in preceding fields. In the controller JS, I set up an object in scope to hold all the form data as follows:
// Angular.js controller function
$scope.formData = {};
In the form's HTML, I backed all the fields with ng-model
as follows:
<html data-ng-app="formApp">
<body>
<form class="form form-horizontal" data-ng-controller="formController">
<div class="form-group">
<label class="control-label" for="username">Username</label>
<input type="text" name="username" data-ng-model="formData.username" class="form-control" data-ng-minlength="3" data-ng-maxlength="8" required>
</div>
</form>
</body>
</html>
So by the time the form is filled, $scope.formData
would have the following structure:
$scope.formData
=>
{ "username": "jdoe",
"roles": [
"Employee", "Lead Engineer", "Coordinator"
],
"location": "Palo Alo, CA",
"and so": "on..."
}
I want to use $http
to asynchronously post the data to an ASP.NET generic HTTP handler, which will process the form submission and respond back in JSON format. What is the recommended approach to post the data? When I use JSON.parse($scope.formData)
, the object is broken into individual request parameters and not submitted as a single object. And if I do get the object across as single object, how do I process the form submitted data? Ideally, I want to convert that entire formData
object into a C# DTO object that I can work with on the server side.