1

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);        
    }

1 Answer 1

2

Yes there is a better way.

In the C# web project, you'll want to add a model class to use instead of the form collection. So lets say your viewmodel is a car, you may want to create a class like this:

public class CarViewModel {
    public string Make { get; set; }
    public string Model { get; set; }
}

You'll want to make sure the property names match those used in the angular view model.

Then, you'll change out your controller action signature from:

public JsonResult Update(FormCollection collection)

To:

public JsonResult Update(CarViewModel viewModel)

Once in there, you can access all the properties directly.

Sign up to request clarification or add additional context in comments.

2 Comments

That was the first thing I tried but for some reason the viewModel is empty when it gets to the server. It is defined but all the values are null.
Verify that the property names match, also, I'd lose the form-url-encoded header, you don't need that in your angular code. Try both of those things and let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.