I have read several of the questions here related to this topic and have a working solution, but I don't know how it works and it seems wrong. New to Angular so need some advice. Posting data from an Angular view model to a C# MVC / .net 4.5 controller.
The angular method to post
$scope.UpdateData = function () {
$http({
method: 'POST',
url: '/Certification/Configuration/' + certId + '/DisplayCategory/Update',
data: $scope.viewModel,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).
success(function (response) {
alert("success");
}).
error(function (response) {
alert("sbf !!!");
});
}
And this is the method in the C# controller
public JsonResult Update(FormCollection collection)
{
string postedValue = Request.Form[0].ToString();
DisplayCategoriesViewModel displayCategories;
JavaScriptSerializer jss = new JavaScriptSerializer();
displayCategories = jss.Deserialize<DisplayCategoriesViewModel>(postedValue);
JsonResult jr = new JsonResult();
jr.Data = "Is SF or UF";
return (jr);
}
Is there a better way? The magic number Request.Form[0] seems wrong. I have tried just posting the viewModel from the browser with json.stringify but just get null.
Any Ideas, suggestions?
Update - for those interested here is the working code
Angular controller
$scope.UpdateData = function () {
$http({
method: 'POST',
url: '/Certification/Configuration/' + certId + '/DisplayCategory/Update',
data: JSON.stringify($scope.viewModel)
}).
success(function (response) {
alert("success");
}).
error(function (response) {
alert("sbf !!!");
});
}
C# Controller
public JsonResult Update(DisplayCategoriesViewModel viewModel)
{
//Do stuff here
JsonResult jr = new JsonResult();
jr.Data = "Is SF or UF";
return (jr);
}