在ASP.NET MVC和angularjs应用程序中,Angularjs控制器没有触发 [英] Angularjs controller is not firing, in ASP.NET MVC and angularjs application

查看:50
本文介绍了在ASP.NET MVC和angularjs应用程序中,Angularjs控制器没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我用以下方式使用ASP.Net MVC和AngularJS,我有一个.Net控制器如下

Hi All,

I am using ASP.Net MVC with AngularJS in the following way, I have a .Net Controller as below

namespace TestAngularJSApplication.Controllers
{
    public class EmployeeController : Controller
    {        
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetEmployees()
        {
            DAL oDAL = new DAL();
            List<Employee> listOfEmployees = oDAL.employees.ToList();
            return Json(listOfEmployees,JsonRequestBehavior.AllowGet);
        }
    }
}



然后我有Index.cshtml如下


Then I have Index.cshtml as below

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
  <meta name="viewport" content="width=device-width" />
   <title>Employee Details</title>
    <script src="~/Scripts/jquery-2.2.4.min.js"></script>
    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/Scripts/myApp.js"></script>
</head>

<body>
    <div data-ng-app="myApp" data-ng-controller="employeeController">
        <table style="border:solid 1px gray;">
            <tr>
                <td style="border:solid 1px gray;">Employee Id</td>
                <td style="border:solid 1px gray;">Employee Name</td>
                <td style="border:solid 1px gray;">Employee Address</td>
                <td style="border:solid 1px gray;">Employee Salary</td>
                <td style="border:solid 1px gray;">Is Active</td>
            </tr>
            <tr data-ng-repeat="emp in employees">
                <td>{{emp.Id}}</td>
                <td>{{emp.Name}}</td>
                <td>{{emp.EmployeeAddress}}</td>
                <td>{{emp.EmpployeeSalary}}</td>
                <td>{{emp.IsActive}}</td>
            </tr>
        </table>
        
    </div>    
</body>
</html>



然后我有myApp.js文件如下


Then I have myApp.js file as below

var myApp = angular.module('myApp', []);
myApp.controller = ('employeeController', function ($scope, $http) {
    $scope.employees = "";
    $http.get("/EmployeeController/GetEmployees")
    .success(function (result) {
        $scope.employees = result;
    })
    .error(function (result) {
        console.log(result);
    });
});



然后我有DAL类,它连接到数据库并为我提供数据。 />


Then I have DAL class which is connecting to the Database and serving me the data.

public class DAL : DbContext
{
    public DAL() : base("TestConnectionString") { }

    public DbSet<Employee> employees { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Employee>().ToTable("EmployeeTable");
        modelBuilder.Entity<Employee>().HasKey(x => x.Id);
    }
}





看到所有问题后,我无法显示视图中表格中的数据。当我检查是否正在调用GetEmployees方法时,它也没有被调用。当我检查angularJS控制器的employeeController时,它也没有被调用。任何人都可以帮我检查为什么employeeController没有调用角度控制器?

我正在谷歌搜索并试图找到缺少的东西,任何类型的帮助将非常有帮助。提前致谢。



我的尝试:



我是谷歌搜索并试图找到遗漏的东西。并尝试保留断点并调试代码。



After seeing all the Problem here is I am not able to display the data in the Table in the View. And when I checked if the GetEmployees method is being called then its not being called as well. When I checked if the "employeeController" of the angularJS controller, then it is also not being called. Can anybody please help me in checking why employeeController the angular controller is not being called?
I am googling and trying to find what is missing, any type of help will be very very helpful. Thanks in advance.

What I have tried:

I am googling and trying to find what is missing. And trying to keep break-points and debug the code.

推荐答案

范围,


http){


scope.employees =;
scope.employees = "";


这篇关于在ASP.NET MVC和angularjs应用程序中,Angularjs控制器没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆