javascript - Angular MVC 项目 - 使用 templateUrl 获取 404

标签 javascript angularjs asp.net-mvc asp.net-mvc-4

我正在尝试从 mvc 项目中的另一个文件夹 (AddEditPersonModal.cshtml) 访问编辑部分,然后通过 Angular 加载 ui 引导模式中的内容。当 index.cshtml 页面加载时,我在该部分上收到 404 错误。这是我的代码:

Angular :

 $scope.editPerson = function (person) {
    $scope.modalModel = person;
    //getModal(person);
    $uibModal.open({
        templateUrl: '/Modal/AddEditPersonModal.cshtml',
        controller: 'modalController',
        scope: $scope
    })
    resolve: {
            person: {
                return $scope.person;
            }
    }
}

索引.cshtml

<a href="" data-toggle="modal" data-target="#myModal" ng-model="editPersonModel" ng-click="editPerson(person)"><span class="fa fa-pencil-square-o"></span></a>

错误:

    angular.js:12011 GET http://localhost:58227/Modal/AddEditPersonModal.cshtml 404 (Not Found)(anonymous function) @ angular.js:12011n @ angular.js:11776(anonymous function) @ angular.js:11571(anonymous function) @ angular.js:16383$eval @ angular.js:17682$digest @ angular.js:17495scopePrototype.$digest @ hint.js:1364$apply @ angular.js:17790scopePrototype.$apply @ hint.js:1427(anonymous function) @ angular.js:25890dispatch @ jquery-1.12.4.min.js:3r.handle @ jquery-1.12.4.min.js:3
angular.js:13920 Error: [$compile:tpload] http://errors.angularjs.org/1.5.8/$compile/tpload?p0=%2FModal%2FAddEditPersonModal.cshtml&p1=404&p2=Not%20Found
    at angular.js:38
    at angular.js:19433
    at angular.js:16383
    at m.$eval (angular.js:17682)
    at m.$digest (angular.js:17495)
    at m.scopePrototype.$digest (hint.js:1364)
    at m.$apply (angular.js:17790)
    at m.scopePrototype.$apply (hint.js:1427)
    at l (angular.js:11831)
    at J (angular.js:12033)

模态 Controller :

 [HttpPost]
    public ActionResult AddEditPersonModal(DTOPerson model)
    {
        return View(model);
    }

最佳答案

在 MVC 中,您导航到返回 View 的 Controller 方法,而不是导航到 View 文件本身。将脚本更改为

$uibModal.open({
    templateUrl: '/Modal/AddEditPersonModal', // amend
    controller: 'modalController',
    scope: $scope
})

并从 AddEditPersonModal() 方法中删除 [HttpPost] 属性(它发出 GET 请求,而不是 POST)

关于javascript - Angular MVC 项目 - 使用 templateUrl 获取 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40815295/

相关文章:

javascript - 选择不显示其默认值

xml - Upshot.js 和 LINQ to XML

c# - 我应该在哪里编写代码来覆盖 MVC 请求的路由数据值?

javascript - Highcharts : Is it possible to customize the colors of individual series?

javascript - 使用 jquery ajax 在 div 中加载外部 php 文件

javascript - Bootstrap Popover 在页面调整大小时不移动

javascript - 无法读取 Angular 移动设备中未定义的属性 'makeCurrent'

javascript - 我可以使用 vue.js v-if 来检查数组中是否存在该值吗

javascript - AngularJS - 像 Google 一样具有逻辑的分页示例

angularjs - 我可以从 mvc 提供静态 html 页面吗