0

So I apologize from the start that I am very new to AngularJS.

So what I am trying to do is pass back my model in my view back to my controller. I tried searching all over the internet with no luck.

Here is what I have right now:

(function() {
    var mod = angular.module('branding', []);

    mod.controller('BrandingController', ['$scope', '$http', function(brandModel) {
        brandModel.model = dataModel.Views;
        brandModel.SelectedName = "a";
        brandModel.SelectedDescription = "s";
        brandModel.SelectedIsBuiltIn = true;

        brandModel.selectItem = function(view) {
            brandModel.SelectedName = view.Name;
            brandModel.SelectedDescription = view.Description;
            brandModel.SelectedIsBuiltIn = view.IsBuiltIn;

        };
        brandModel.clearText = function() {
            brandModel.SelectedName = "";
            brandModel.SelectedDescription = "";
            brandModel.SelectedIsBuiltIn = "";
        };

        brandModel.update = function($http) {
            brandModel.apply(function() {
            $http.post("@Url.Action("SaveBranding","AirlineConfig")");
            //.success and .fail never are triggered when implemented 
            });
        };
    }]); 
})();    

My Controller

   [HttpPost]
    public ActionResult SaveBranding(BrandingViewModel viewModel)
    {
        return View("Branding", viewModel);
    }

I am able to trigger the call of update no problem but I see nothing at all from the http.post call.

I have tried just a call directly to the server via a post and to a direct path and that didnt work either.

11
  • What does the network request/response look like in the debug console for /AirlineConfig/SaveBranding? Commented Feb 23, 2015 at 23:33
  • Actually the network tab showed nothing but the console did show this: Unable to get property 'post' of undefined or null reference Commented Feb 23, 2015 at 23:37
  • 2
    You're missing $http in the controller function...you just have it in the dependency list. Commented Feb 23, 2015 at 23:42
  • 1
    I think you also need remove $http from the update function: brandModel.update = function() { Commented Feb 23, 2015 at 23:52
  • 1
    You made the second change as well where $http was getting redefined in the update function? Commented Feb 24, 2015 at 0:00

1 Answer 1

1

You should add property:

mod.controller('BrandingController', ['$scope', '$http', function(brandModel, $http) {

and invoke like that:

 brandModel.update = function() {
            $http.post("@Url.Action("SaveBranding","AirlineConfig")", brandModel.model);
            //.success and .fail never are triggered when implemented 
        };

Where your brandModel.model is BrandingViewModel.

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

6 Comments

I have change my script to be this and i still get a TypeError: Unable to get property 'post' of undefined or null reference
In the end i needed to remove the $http in the update function
Ooo, Sorry I mad a mistake. Remove $http from 'brandModel.update = function($http) {'
now when I pass the model back to the controller, do i need to convert it back into the viewmodel type?
You need pass an object with the same fields as the viewmodel. If your viewmodel has a property like 'public Name {get;set;}' you should pass javascript object with 'Name' field.
|

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.