使用角度js列出数据库中的数据

时间:2017-05-21 17:26:55

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

我正在尝试使用角度jsDat来列出数据,但是什么可以在那里写“标记中的数据”?

我的控制器:

 public ActionResult Index()
    {
        var list = db.kayitlar.ToList();
        string markers = "[";
        foreach (var item in list)
        {
            markers += "{";
            markers += item.name;
            markers += item.surname;
            markers += item.money;
            markers += item.country;
            markers += "},";
        }
        markers += "];";
        ViewBag.Markers = markers;

        return View(db.kayitlar.ToList());


    }

和我的观点

@{

var markers = Html.Raw(ViewBag.Markers);
 }

<table class="table table-bordered table-striped text-center">
    <tr>
        <th>name</th>
        <th>surname</th>c
        <th>money</th>
        <th>country</th>
    </tr>

    <tr ng-repeat="data in markers">
        <td>{{data.name|uppercase}}</td>
        <td>{{data.surname|lowercase}}</td>
        <td>{{data.money|number:3}}</td>
        <td>{{data.country}}</td>

    </tr>
</table>

这里是kayitlar班级

public class kayit
{
    public int id { get; set; }
    public string name { get; set; }
    public string surname { get; set; }
    public double money { get; set; }
    public string country { get; set; }
}

SQL Server表:

enter image description here

我该如何解决这个问题?我是学生。请帮助我。

1 个答案:

答案 0 :(得分:0)

AngularJS's ng-repeat需要一个数组。根据您的问题,您的JSON必须是例如:

[{"name":"Danny","surname":"Surename","money":19000.0,"country":"Perú"},{"name":"Danny2","surname":"Surename2","money":14000.0,"country":"Austrlia"}]

服务器端:

要渲染此JSON,您可以使用Newtonsoft.Jsonusing Newtonsoft.Json;)。

<强>控制器:

[HttpGet]
public ActionResult Index()
{
    ViewBag.data = buildJson(getData());
    return View();
}
private List<kayit> getData()
{
    List<kayit> lkayit = new List<kayit>();
    kayit oKayitlar;
    oKayitlar = new kayit();
    oKayitlar.name = "Danny";
    oKayitlar.surname = "Surename";
    oKayitlar.money = 19000;
    oKayitlar.country = "Perú";
    lkayit.Add(oKayitlar);

    oKayitlar = new kayit();
    oKayitlar.name = "Danny2";
    oKayitlar.surname = "Surename2";
    oKayitlar.money = 14000;
    oKayitlar.country = "Austrlia";
    lkayit.Add(oKayitlar);
    return lkayit;
}

private string buildJson(List<kayit> list)
{
    var markers = JsonConvert.SerializeObject(list);
    Console.Write(markers); // Prints [{"name":"Danny","surname":"Surename","money":19000.0,"country":"Perú"},{"name":"Danny2","surname":"Surename2","money":14000.0,"country":"Austrlia"}]
    return markers;
}

<强>前端

查看:

<body data-ng-controller="Controller">
    <input id="hdnData" type="hidden" value="@ViewBag.data" /> <!-- [{"name":"Danny","surname":"Surename","money":19000.0,"country":"Perú"},{"name":"Danny2","surname":"Surename2","money":14000.0,"country":"Austrlia"}] -->
    <table class="table table-bordered table-striped text-center">
      <tr>
        <th>name</th>
        <th>surname</th>
        <th>money</th>
        <th>country</th>
      </tr>

      <tr ng-repeat="data in markers">
        <td>{{data.name|uppercase}}</td>
        <td>{{data.surname|lowercase}}</td>
        <td>{{data.money|number:3}}</td>
        <td>{{data.country}}</td>
      </tr>
    </table>
</body>

<强> AngularJS:

(function() {
    var app = angular.module("app", []);
    app.controller("Controller", ["$scope", function($scope) {
      $scope.data = document.getElementById("hdnData").value;
      if ($scope.data.length > 0) {
        $scope.data = JSON.parse($scope.data);
        $scope.markers = $scope.data;
      }
    }]);
})();

这是demo

希望这有帮助。

更新:使用ADO.NET从SQL Server数据库获取数据。

SQL Server存储过程:

CREATE PROCEDURE [dbo].[USP_GET_KAYIT_LIST]
AS
BEGIN
    SET NOCOUNT ON
    SELECT name, surename, money, country FROM kayitTable
    SET NOCOUNT OFF
END
GO

<强> ADO.NET:

<强> blkayit.cs

// BL = Business Layer
public class blkayit
{
    public List<kayit> GetkayitList()
    {
        List<kayit> lkayit = null;
        string ConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password = myPassword; ";
        using (SqlConnection con = new SqlConnection(ConnectionString))
        {
            try
            {
                con.Open();
                dlkayit odlkayit = new dlkayit();
                lkayit = odlkayit.GetkayitList(con);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
        return lkayit;
    }
}

<强> dlkayit.cs

// DL = Data Layer
public class dlkayit
{
    public List<kayit> GetkayitList(SqlConnection con)
    {
        List<kayit> lkayit = null;
        SqlCommand cmd = new SqlCommand("USP_GET_KAYIT_LIST", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 1800;
        SqlDataReader drd = cmd.ExecuteReader(CommandBehavior.SingleResult);
        if (drd != null)
        {
            lkayit = new List<kayit>();
            kayit okayit;
            while (drd.Read())
            {
                okayit = new okayit();
                okayit.name = drd.GetString(0);
                okayit.surename = drd.GetString(1);
                okayit.money = drd.GetDecimal(2);
                okayit.country = drd.GetString(3);
                lkayit.Add(okayit);
            }
            drd.Close();
        }
        return lkayit;
    }
}

然后在你的控制器中:

private List<kayit> getData()
{
    List<kayit> lkayit = blKayit.GetkayitList();
    return lkayit;
}