【问题标题】:ASP MVC http.post calling Action to Controller with angular not workingASP MVC http.post 调用动作到控制器,角度不起作用
【发布时间】:2015-04-25 11:23:24
【问题描述】:

所以我从一开始就道歉,我对 AngularJS 很陌生。

所以我要做的就是将我视图中的模型传回给我的控制器。我尝试在互联网上进行搜索,但没有成功。

这是我现在拥有的:

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

我的控制器

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

我能够触发更新调用没有问题,但我从 http.post 调用中什么也没看到。

我已经尝试通过帖子和直接路径直接调用服务器,但这也不起作用。

【问题讨论】:

  • /AirlineConfig/SaveBranding 的调试控制台中的网络请求/响应是什么样的?
  • 实际上网络选项卡什么也没显示,但控制台确实显示:无法获取未定义或空引用的属性“帖子”
  • 你在控制器函数中缺少 $http...你只是在依赖列表中有它。
  • 我认为您还需要从更新函数中删除 $http:brandModel.update = function() {
  • 您在更新函数中重新定义 $http 的位置也进行了第二次更改?

标签: asp.net .net asp.net-mvc angularjs


【解决方案1】:

你应该添加属性:

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

然后像这样调用:

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

您的brandModel.model 是BrandingViewModel。

【讨论】:

  • 我已经把我的脚本改成了这个,但我仍然得到一个 TypeError: Unable to get property 'post' of undefined or null reference
  • 最后我需要删除更新功能中的$http
  • 噢,对不起,我犯了一个错误。从 'brandModel.update = function($http) {' 中删除 $http
  • 现在当我将模型传回控制器时,是否需要将其转换回视图模型类型?
  • 您需要传递一个与视图模型具有相同字段的对象。如果你的 viewmodel 有一个像“public Name {get;set;}”这样的属性,你应该传递带有“Name”字段的 javascript 对象。
猜你喜欢
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2012-09-14
  • 1970-01-01
  • 2017-11-10
  • 2015-06-02
相关资源
最近更新 更多