0

I am new to Angular.js framework. I am getting data which i am further assigning to array using angular scope.

Modal.CS:

 public class AppSetting1
{
    public int lng_AppSettings { get; set; }
    public string str_SettingName { get; set; }
    public string str_SettingValue { get; set; }
    public string str_Type { get; set; }

    public AppSetting1(int _lng_AppSettings, string _str_SettingName, string _str_SettingValue, string _str_Type)
    {
        lng_AppSettings = _lng_AppSettings;
        str_SettingName = _str_SettingName;
        str_SettingValue = _str_SettingValue;
        str_Type = _str_Type;
    }
}

 internal string GetAppSettings()
        {
            try
            {

                    List<AppSetting1> objAppsettings = new List<AppSetting1>();
                    objAppsettings.Add(new AppSetting1(1,"Name1","Value1","Type"));
                    objAppsettings.Add(new AppSetting1(2, "Name2", "Value2", "Type2"));
                    return JsonConvert.SerializeObject(objAppsettings, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Controller.CS:

   [AuthCheckService, SessionCheckService]
    [HandleModelStateException]
    public string GetAppSettings()
    {
        try
        {
           ManageAppSettings accExec = new ManageAppSettings();
           return accExec.GetAppSettings();
        }
        catch (Exception ex)
        {
            throw new ModelStateException(ex.Message, ex.InnerException);
        }
    }

    [HttpPost]
    public JsonResult SaveSettings(List<AppSetting1> AppSetting)
    {
        try
        {
            ManageAppSettings accExec = new ManageAppSettings();
            return Json(AppSetting, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw new ModelStateException(ex.Message, ex.InnerException);
        }
    }

Angular.js:

(function () {
var app = angular.module('myAppSeetings', []);
app.controller('AppSettingsController', function ($scope) {

    $scope.items = [];
    $scope.SaveSettings = function () {
        if (validate()) {
            var token = $('[name=__RequestVerificationToken]').val();
            var test = $scope.items;
            $.ajax({
                beforesend: showProgress(),
                type: 'POST',
                headers: { "__RequestVerificationToken": token },
                url: getAppPath() + 'AppSettings/SaveSettings',
                dataType: 'json',
                data: { AppSetting: $scope.items },
                success: function (result) {
                    if (result != "") {
                        //ShowSaveMessage(result);
                        //fetchData();
                        //$('#EditPopUp').css('display', 'none');
                        //$('#exposeMaskManageUser').css('display', 'none');
                        //clearForm();
                    }
                    else
                        ShowErrorPopup('An error has occurred. Please contact System Administrator.');
                },
                complete: hideProgress,
                error: function (ex) {
                    ShowErrorPopup('An error has occurred. Please contact System Administrator.');
                }
            });
        }
        else {
            ShowWarningMessage('Required fields must be completed prior to completing the work');
        }
    };

    function fetchData() {

        var token = $('[name=__RequestVerificationToken]').val();
        $.ajax({
            beforesend: showProgress(),
            type: 'GET',
            headers: { "__RequestVerificationToken": token },
            url: getAppPath() + 'AppSettings/GetAppSettings',
            dataType: 'json',
            success: function (data) {
                // console.log(data);
                $scope.items = data;
                $scope.$apply();
                console.log($scope.items);
            },
            complete: hideProgress,
            error: function (ex) {
                ShowErrorPopup('An error has occurred. Please contact System Administrator.');
            }
        });
    };

    function validate() {
        var val = true;
        if ($("input").val().trim() == "") {
            val = false;
        }
        return val;
    }
    fetchData();
});

})();

Problem:

On save click i am getting null on server side. Where i am going wrong here?

1

2 Answers 2

1

Try adding contentType: 'application/json; charset=utf-8',

See this answer

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

1 Comment

Thanks a lot Damian. I forgot to add this :-)
0

Your code wrong here:

data: { AppSetting: $scope.items }

It should be

data: $scope.items

In your saveClick function: $scope.items now is []. It should have some values as you expect. Depend on your case it is from client or a default value for testing:

$scope.items = [{lng_AppSettings: 1, str_SettingName : 'Name 1'},
                {lng_AppSettings: 2, str_SettingName : 'Name 2'}];

3 Comments

still getting null
Nothing, On server when i look into parameter values then it is null. On client side everything working fine. There is no exception or error log there.
$scope.items is empty now. it should have value

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.