无法使用AngularJS和MVC获得输出

时间:2015-09-20 13:38:33

标签: angularjs asp.net-mvc

我是asp.net mvc和angularjs的新手。我遇到了一段我无法获得输出的代码。您是否可以建议以下错误:

这是我的代码:
我。 Module.js
Maximum number to factorise: 20 * - - - - - - - - - - - - - - - - - - - * * - - - - - - - - - - - - - - - - - - * - * - - - - - - - - - - - - - - - - - * * - * - - - - - - - - - - - - - - - - * - - - * - - - - - - - - - - - - - - - * * * - - * - - - - - - - - - - - - - - * - - - - - * - - - - - - - - - - - - - * * - * - - - * - - - - - - - - - - - - * - * - - - - - * - - - - - - - - - - - * * - - * - - - - * - - - - - - - - - - * - - - - - - - - - * - - - - - - - - - * * * * - * - - - - - * - - - - - - - - * - - - - - - - - - - - * - - - - - - - * * - - - - * - - - - - - * - - - - - - * - * - * - - - - - - - - - * - - - - - * * - * - - - * - - - - - - - * - - - - * - - - - - - - - - - - - - - - * - - - * * * - - * - - * - - - - - - - - * - - * - - - - - - - - - - - - - - - - - * - * * - * * - - - - * - - - - - - - - - *

II。 Controller.js

var app = angular.module("myApp", []);

III。 Service.js

app.controller("myCntrlr", function ($scope, angularService) {    
    GetAllEmployee();

    function GetAllEmployee(){
        var getData = angularService.getEmployees();
        getData.then(function (emp) {
            $scope.employees = JSON.parse(emp.data);
        }, function () {
            alert('Error in getting records');
        });
    }
});  

:一种。 HomeController.cs

app.service("angularService", function ($http) {
    this.getEmployees = function () {
        return $http.get("Home/GetAll");
    };
});


A(i)中。 EmployeeDAL.cs (我的DataAccessLayer)

    public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult GetAll()
    {
        EmployeeDAL objDAL = new EmployeeDAL();
        var employeeList = objDAL.SelectAllData();
        return Json(employeeList, JsonRequestBehavior.AllowGet);
    }
}

B中。 Index.cshtml

public string SelectAllData()
    {
        SqlConnection conn;
        DataTable dt = new DataTable();
        String json = String.Empty;
        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            dt.Load(dr);
            json = GetJson(dt);
            conn.Close();
            return json;                
        }
        catch
        {
            return json;
        }
    }

    // Method to convert Datatable to JSON Array
    public string GetJson(DataTable dt)
    {
        JavaScriptSerializer JSSerializer = new JavaScriptSerializer();
        List<Dictionary<String, Object>> DtRows = new List<Dictionary<String, Object>>();
        Dictionary<String, Object> newRow = null;

        // Code to loop through each row in datatable and add it to the dictionary object
        foreach(DataRow drow in dt.Rows)
        {
            newRow = new Dictionary<String, Object>();
            foreach(DataColumn col in dt.Columns)
            {
                newRow.Add(col.ColumnName.Trim(), drow[col]);
            }
            DtRows.Add(newRow);
        }
        // Serialising the dictionary object to produce json
        return JSSerializer.Serialize(DtRows);
    }<br/>


另外,我一直收到以下错误 的 [ngRepeat:愚弄]
还有一个问题:是否需要在Index.cshtml中注释掉 @model AngularCRUD.Models.Employee

2 个答案:

答案 0 :(得分:0)

检查employees数组中是否有任何数据。

并且不要一起使用Asp.Net视图和角度。使用其中一个。

答案 1 :(得分:0)

对于错误[ngRepeat:dupes]

Angular disallow duplication

From angular code

throw ngRepeatMinErr('dupes',
              "Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}",
              expression, trackById, value);

因此,错误表明您需要使用track by

    <tr ng-repeat="employee in employees track by $index">